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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With