Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change label text before file read happens

I haven't used java in awhile and I am trying to do something like this in one main class:

// do stuff
this.label.setText("Status: IDLE");
// do things
this.label.setText("Status: LOADING..."); // set to loading right before file read
// read and parse huge file
this.label.setText("Status: DONE");

I want the label to show 'loading...' as the large file read is being executed but the label never freezes (as the job is being ran). How can I force the label to change during read? Do I need to use separate class/thread? Thanks in advance for your help.

like image 464
Shay Anderson Avatar asked Jan 18 '26 20:01

Shay Anderson


1 Answers

How can I force the label to change during read? Do I need to use separate class/thread?

Yes, you are right. The label and the class that is changing it's value should be in different threads.

Have a look at SwingUtilities invokeLater:

Runnable newThread = new Runnable() {
     public void run() {
     }
 };

You can create a thread this way showed above.

like image 109
Heisenbug Avatar answered Jan 20 '26 10:01

Heisenbug



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!