#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2009
    Location
    London, ON
    Posts
    322
    Rep Power
    214

    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
    Originally Posted by DaWei_M
    That covers a multitude of your sins, right there.
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    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.
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2009
    Location
    London, ON
    Posts
    322
    Rep Power
    214
    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?
    <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
    Originally Posted by DaWei_M
    That covers a multitude of your sins, right there.
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    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.
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2009
    Location
    London, ON
    Posts
    322
    Rep Power
    214
    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.
    <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
    Originally Posted by DaWei_M
    That covers a multitude of your sins, right there.
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    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.

IMN logo majestic logo threadwatch logo seochat tools logo