开发者

How to update a label to make it display new text?

开发者 https://www.devze.com 2023-01-09 02:29 出处:网络
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: Jus

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.
stat开发者_JAVA技巧usLabel.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.


CODe's answer is right, but I'd go with the SwingWorker class:

An abstract class to perform lengthy GUI-interacting tasks in a dedicated thread.

It's the right tool for your problem.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号