3
I want to try to solve the problem of when the client cannot connect to the server(connection failure, server below, etc).
In my main class is the connection to the server and I want in case of error caught by try ... catch(ConnectException ex)
launch a JDialog
to inform the client that it is not yet connected...
Something like this, Jdialog is created but is only designed when the operation is over, that is while the main thread tries to connect does not draw anything, only when it gets the connection does it draw both the Jdialog and the main frame.
Code to connect to 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 appear Jdialog already tried:
Creating a normal class, but it didn’t work;
Creating a thread that creates Jdialog, also failed
Now last I’m trying with the swingworker, but with some problems...
Questions:
- SwingWorker is the path I must follow?
- What should I insert as input parameters
SwingWorker<Integer, Integer>
- It is at Inbground that I must create Jdialog or the constructor?
EDIT I have serious doubts whether my problem is in multithreading or the way java processes what I want to do...
Sketch:
I don’t know if it helps this edition...
I asked a question more or less on the same subject, see if it helps you: http://answall.com/q/38078/14674
– Franchesco
The program is not falling into any Exception ?
– urb
@Andrélizardo not, is not, the code is all working, inside
doInBackground
I created a for loop to make printf and these printfs shows on the console, but Jdialog does not (just draw at the end)– jsantos1991
@Earendul may say so, but my problem is just before yours, how did you create Jdialog inside Stringworker? my big problem is, if I run Jdialog alone it works, but if I call it inside Stringworker Jdialog is only visible after the process of connecting to the server which is when the client form appears too...
– jsantos1991
You can answer your own question with the solution you found, ;)
– Franchesco
@Earendul many apologies, the problem is STILL not solved, remains exactly the same, it seemed but I was wrong
– jsantos1991