The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Java Help
|
Swing - JLabel update is locked out by continuation of program
Discuss JLabel update is locked out by continuation of program in the Java Help forum on Dev Shed. JLabel update is locked out by continuation of program Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 26th, 2013, 02:54 AM
|
 |
Contributing User
|
|
Join Date: Oct 2009
Location: London, ON
|
|
|
Swing - JLabel update is locked out by continuation of program
Hello!
I have a JLabel in a JDialog to show users what's going on, but sometimes it updates out of order or too late to be of use.
Code:
JLabel lZipStatus;
lZipStatus.setText("Installing, please wait...");
unzipFile(zFile);
lZipStatus.setText("Installation Complete.");
Often the wait message appears after the zip file has already begun executing, or it appears AFTER the "Installation Complete" message, effectively overwriting it. What can I do to avoid this behaviour?
__________________
<Tetrad> the program I just wrote 1) compiled the first time without any errors and 2) worked like it was supposed to
<Tetrad> I don't know whether to be proud or scared to death
Quote: | Originally Posted by DaWei_M That covers a multitude of your sins, right there. |
|

January 26th, 2013, 06:16 AM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: Eastern Florida
|
|
|
Are you talking about calls to setText() only showing what was passed in the last call to setText()? The JVM doesn't get to update the GUI after each call to setText(). When it gets control the earlier calls to setText() are gone and ony the last is used.
Is the code being executed on the EDT? That locks the JVM out from being able to update the GUI.
|

January 26th, 2013, 02:55 PM
|
 |
Contributing User
|
|
Join Date: Oct 2009
Location: London, ON
|
|
Quote: | Is the code being executed on the EDT? That locks the JVM out from being able to update the GUI. |
It's being executed under "bAddModButtonActionPerformed" method, so I guess so?
|

January 26th, 2013, 04:32 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: Eastern Florida
|
|
|
If you start a new thread to do the long task and exit the action listener, the EDT will be able to update the GUI to show the waiting message. When the long task ends, it can update the GUI with the completed message.
|

January 26th, 2013, 09:59 PM
|
 |
Contributing User
|
|
Join Date: Oct 2009
Location: London, ON
|
|
So THAT'S what this code is doing:
Code:
java.awt.EventQueue.invokeLater(new Runnable() {
mainFrame frame = new mainFrame();
@Override
public void run() {
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
I'm using pre-gen'd code from NetBeans, but I didn't understand why I needed this in my main. It all makes sense now!
So basically just create a new Runnable object in my ActionPerformed, override run() with the call to "unzipFile()" and run it immediately?
EDIT: Also needed to create an actual Thread!
Code:
lZipStatus.setText("Installing, please wait...");
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
unzipFile(tfZipPath.getText());
}
});
thread.start();
I had a large large portion of unzipFile() method being executed within the ActionPerformed. It was iterating through the ZipFile and for each entry it was sending that entry to another method called "writeZipToFile". The way you've explained it, I've offloaded anything that would lock the GUI to another thread, effectively allowing the user to interact with the GUI while loops and other things are running. Not something I had ever thought about, but I'll be sure to implement in this way going forward.
Thanks a ton! 
Last edited by Wetmelon : January 27th, 2013 at 01:19 AM.
|

January 27th, 2013, 06:01 AM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: Eastern Florida
|
|
|
You might want to disable some of the buttons while the long running task is executing to keep the user from starting another one at the same time.
Then enable the button when its ok to do it again.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|