views:

66

answers:

1

I am making an application in netbeans and want to have a status label that tells what is going on in the program at any given moment. There is a ton of code, but here is pretty much what it does: Just pretend that statusLabel is a label that has already been put in the program and each of the functions is an expensive function that takes a few seconds.

statusLabel.setText("Completing Task 1");
System.out.println("Completing Task 1");
this.getFrame().repaint(); //I call this function and the two functions below it but the label still does not change.
statusLabel.updateUI(); //Doesn't seem to do much.
statusLabel.revalidate(); //Doesn't seem to do much.
this.completeTask1();
statusLabel.setText("Completing Task 2");
System.out.println("Completing Task 2");
statusLabel.revalidate();
this.getFrame().repaint();
...

This goes on until the UI has completed 4 tasks. During the entire process the label does not update until after every single task has been completed, and then it says "Completing Task 4". The System.out.println calls work perfectly though. Basically I am wondering what I should do to make the label show the new text that it has been set to.

+4  A: 

Never use the updateUI() method. This is not necessary.

You also don't need to use revalidate(). Swing will automatically repaint a component when you change its properties.

the label does not update until after every single task has been completed, and then it says "Completing Task 4".

It is because you are blocking the EDT which will prevent Swing from repainting the component until the long running task is finished.

Read the section from the Swing tutorial on Concurrency for a complete descrition and solution.

camickr