I can not display the value received from the server

0

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

My client is made up of Graphical Interfaces with Swing and the server working with multi-threads. But I'm at a point where I can not go through with it. There is an action on the server where it sends a data (String) every 10 seconds to the client. The problem is at the moment when I will receive this data and set it on a label located in a Jframe. I can not get the data and set it at the same time. Here's the snippet where I get 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 have already done several tests and I have 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 only receives once the value of the server and gets the value of the label on the label and is updated on the screen, but I must always take these values until the while stop condition comes.     

asked by anonymous 08.06.2017 / 16:17

1 answer

1

You may be generating an infinite loop with this while, and the program gets so busy running while it does not have time to refresh the screen.

Try putting the following command into the while in the first

Thread.sleep(1000);

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

    
08.06.2017 / 17:03