Results 1 to 4 of 4
  1. #1
    Xiphias is offline Member
    Join Date
    Mar 2009
    Posts
    2
    Rep Power
    0

    Default Updating a JProgressBar from another thread?

    So I have two objects at play, here - one is a class that extends Thread (MyThread) and is doing some parsing work. The other class extends JDialog (MyDialog) and contains a JProgressBar.

    The parsing thread, MyThread, periodically calls a method in MyDialog from the thread's run() method.

    the method it calls is:
    Java Code:
    	public void progressUpdate(int percent) {
    		synchronized(progressLock){
    			progressBar.setValue(percent);
    		}
    		SwingUtilities.invokeLater(
    				new Runnable() {
    					public void run() 
    					{
    						progressBar.updateUI();
    						logger.debug("updated");
    						}
    					}
    				);
    		logger.debug("invoked an update");
    	}
    When this runs, two things seem to go wrong - first, none of the Runnables call UpdateUI() until the parsing thread is complete. Secondly, the entire MyDialog window doesn't draw itself, at all. Any thoughts?

  2. #2
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    I see what you are doing. You have the right code, but the wrong execution of the concept.

    *Never* update a Component from a thread other than the EDT. You are correct to use invokeLater() to execute code on the EDT. However, the code executed on the EDT should do setValue(), and there is no need to synchronize.

    You may need to make your parameter "percent" final in order to access it from inside the anon. inner class. I don't believe the updateUI() call is necessary.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I have a feeling that what you think is running on a background thread really isn't, your parsing is running on the EDT (there's a static method in the SwingUtilities that will let you know if I am right). If this is the problem then you'll have to take greater care that what you want to have happen on a background thread is in fact called on a background thread. Perhaps you want to use a SwingWorker object here?

  4. #4
    Xiphias is offline Member
    Join Date
    Mar 2009
    Posts
    2
    Rep Power
    0

    Default

    Thank you both for the help - turns out you were both right. Steve was right about moving the setValue() method for my progress bar into the Runnable, and Fubarable was right too - turns out I was "starting" my thread by calling run() instead of start(). Doh.

    Thanks, guys!

Similar Threads

  1. Placing a JProgressBar over my JPEG image
    By hitmen in forum AWT / Swing
    Replies: 7
    Last Post: 03-08-2009, 11:16 AM
  2. [SOLVED] Netbeans Desktop App & JProgressBar
    By SebScoFr in forum NetBeans
    Replies: 3
    Last Post: 11-28-2008, 12:00 AM
  3. Updating a progress bar from the UI thread
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-11-2008, 05:52 PM
  4. Updating a SWT progress bar from another thread
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-11-2008, 05:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •