Results 1 to 4 of 4
- 03-10-2009, 07:07 AM #1
Member
- Join Date
- Mar 2009
- Posts
- 2
- Rep Power
- 0
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:
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?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"); }
- 03-16-2009, 01:47 PM #2
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.
-
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?
- 03-18-2009, 01:48 AM #4
Member
- Join Date
- Mar 2009
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
Placing a JProgressBar over my JPEG image
By hitmen in forum AWT / SwingReplies: 7Last Post: 03-08-2009, 10:16 AM -
[SOLVED] Netbeans Desktop App & JProgressBar
By SebScoFr in forum NetBeansReplies: 3Last Post: 11-27-2008, 11:00 PM -
Updating a progress bar from the UI thread
By Java Tip in forum SWTReplies: 0Last Post: 07-11-2008, 04:52 PM -
Updating a SWT progress bar from another thread
By Java Tip in forum SWTReplies: 0Last Post: 07-11-2008, 04:51 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks