I cannot display the value received from the server

Asked

Viewed 36 times

0

I am working on a project written in Java with a Client structure - Server.

My client consists of Swing Graphical Interfaces and the server working with multi-threads. But I’m at a point where I can’t go through with it. There is an action on the server where it sends a data (String) to the client every 10 seconds. The problem is at the moment when I will receive this data and set in a label located in a Jframe. I am not able to receive the data and set at the same time. Follow the section where I receive and I set the value:

while (cond) {
    //Espera ate receber um dado do srv para ler
String line = in.readLine(),pedra;
    //Verifica se incia com PEDRA. True = Pego valor, setText
    if ( line.startsWith("PEDRA")) {
        pedra = line.substring(6));
        lblMostraDado.setText(pedra); 
//Caso for BINGO fim de jogo
}else if( line.startsWith("BINGO")){
cond = false;
}

I’ve done a lot of tests and I’ve drawn some conclusions. When I run the above code in debug mode the values arrive from the server and are assigned to the label normally, but it is not updated in the window. Now when I remove this while, it receives only once the value of the server and gets arrow the value on the label and is updated on the screen, however I need to be always picking these values until the stop condition of the while comes.

1 answer

1


You may be generating an infinite loop with that while, and the program gets so busy running while it doesn’t have time to update the screen.

Try placing the following command inside while on the first

Thread.sleep(1000);

Do the Try/catch if necessary. This will make the program "sleep" for 1 second with each loop iteration, giving time to update the screen.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.