strange bug - how to pause a java program?

Posted by TerraNova993 on Stack Overflow See other posts from Stack Overflow or by TerraNova993
Published on 2012-03-24T16:31:19Z Indexed on 2012/03/24 17:29 UTC
Read the original article Hit count: 143

Filed under:
|
|

I'm trying to:

  1. display a text in a jLabel,
  2. wait for two seconds,
  3. then write a new text in the jLabel

this should be simple, but I get a strange bug: the first text is never written, the application just waits for 2 seconds and then displays the final text. here is the example code:


private void testButtonActionPerformed(java.awt.event.ActionEvent evt) {    
    displayLabel.setText("Clicked!");

    //  first method with System timer

    /*
    long t0=  System.currentTimeMillis();
    long t1=  System.currentTimeMillis();
            do{
                t1 = System.currentTimeMillis();
            }
            while ((t1 - t0) < (2000));
     */    

    // second method with thread.sleep()

    try {
        Thread.currentThread().sleep(2000);
    } catch (InterruptedException e) {}

    displayLabel.setText("STOP"); 
}

with this code, the text "Clicked!" is never displayed. I just get a 2 seconds - pause and then the "STOP" text. I tried to use System timer with a loop, or Thread.sleep(), but both methods give the same result.

© Stack Overflow or respective owner

Related posts about java

Related posts about swing