I want to try to solve the problem of when the client can not connect to the server (connection failure, server down, etc.).
In my main class is the connection to the server and I want that in case of giving error caught by try ... catch(ConnectException ex)
throw a JDialog
to inform the client that is not yet connected ...
JDialogisnotaJDialogobject,butitisaJDialogobject.
Code to connect to the server:
public Socket connect() {
try {
this.socket = new Socket("localhost", 5555);
this.output = new ObjectOutputStream(socket.getOutputStream());
} catch (ConnectException ex) {
waitinng();//crio o frame mas ele só aparece quando o servidor está conectado
System.out.println("tenta conectar");
connect();//volto a chamar a função até que se consiga conectar
Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
} catch (UnknownHostException ex) {
Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
}
return socket;
}
To display JDialog I've already tried:
Create a normal class, but it did not work;
Create a thread that creates JDialog, also did not give
Now finally I'm trying with the swingworker, but with some problems ...
Questions:
- Is swingWorker the way to go?
- What should I enter as input parameters
SwingWorker<Integer, Integer>
- Is it in doInBackground that I should create JDialog or the builder?
EDIT I have serious doubts if my problem is in the multithread or the way java handles what I want to do ...
Outline:
I do not know if this edition helps ...