wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Help! Problem updating JProgressBar. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Help! Problem updating JProgressBar." Watch "Help! Problem updating JProgressBar." New topic
Author

Help! Problem updating JProgressBar.

Dan Bizman
Ranch Hand

Joined: Feb 25, 2003
Posts: 387
Can someone help me here. When I click a button (which then does a number of things including calling the progressBar.setValue( int ) every now and then), the whole thing basically hangs until it's done. Then the progress bar shows 100%. HOWEVER, if I include a call to: JOptionPane.showMessageDialog( ( Component ) null, "mess" ); every time I set the value, THEN (and only then) it updates the progress bar. What am I doing wrong?

I've tried a number of things, but right now I add the progress bar to a new frame and then have this:



Notifiable is just a class I created so my object can just call that every time it wants to update. How do I fix this?
Dan Bizman
Ranch Hand

Joined: Feb 25, 2003
Posts: 387
Well, I'm sure this is NOT the best way to do this but I got it to work by doing the following:





But the problem is now it creates a new thread EVERY time there's a change! How do I get this to work by just opening ONE other thread that handles this? (keep in mind that I have to keep calling "notify( int )" in my main code to tell it to update something. Do I make a new class that implements Notifiable and is started in a new thread? If so, how do I keep it running and yet have it respond to calls to notify( int )?
Dan Bizman
Ranch Hand

Joined: Feb 25, 2003
Posts: 387
OK, I finally got it to work! The key is two things:

1. Kick off a new thread that loops while sleeping (Thread.sleep( 100 ) or whatever) and monitors value changes

2. When you make the change to the progress bar, you MUST call the following:



If you don't repaint it immeadiately, then the changes never show up.

hope this helps someone!
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Actually, the key here is that all intense work should be done outside of the Event Dispatching Thread. If your dispatch thread is busy waiting for something that is going to take a while, it can never get around to doing what it was designed for. And part of that is repainting the GUI for you.

This is the best thing I have ever seen on Swing threading issues. I learned more from the 20 minutes watching it than 5 years of reading. Ok, that's an exagerationg, but you get the idea.


GenRocket - Experts at Building Test Data
Alexandru Popescu
Ranch Hand

Joined: Jul 12, 2004
Posts: 995
I agree with you Greg: *best thing seen* :-). The documentation - if read - is same as clear as that (long over a weak connection) demo.

--
./pope
[the_mindstorm]


blog - InfoQ.com
Alexandru Popescu
Ranch Hand

Joined: Jul 12, 2004
Posts: 995
And a complition on slide 19: a better way. I can say there are 2 more options : Spin and one proposed by Ramnivas in his book AspectJ in Action (involving aop).

--
./pope
[the_mindstorm]
Dan Bizman
Ranch Hand

Joined: Feb 25, 2003
Posts: 387
Thanks for the replies. A few things:

1. Looking at SwingWorker, I see that I basically did the same thing except I didn't abstract it quite as much (My worker class had a few methods that were more specific to my project). It's a good idea.

2. I have not yet seen a good way to CLEANLY have a class send notifications about its progress. I find I have to litter the code with calls to "notify()" throughout its processing methods in order to update something like a progress bar. Is there a better way to do this? Some way for a separate object to "register" with the JVM or something to keep figuring out where some object is in it's method calls? There just has to be something better.
(This is really a diff. question, so I posted it on it's own in Java-General. Everyone can respond there: Registering for Object Progress?)
[ January 21, 2005: Message edited by: Dan Bizman ]
Alexandru Popescu
Ranch Hand

Joined: Jul 12, 2004
Posts: 995
Originally posted by Dan Bizman:

2. I have not yet seen a good way to CLEANLY have a class send notifications about its progress. I find I have to litter the code with calls to "notify()" throughout its processing methods in order to update something like a progress bar. Is there a better way to do this? Some way for a separate object to "register" with the JVM or something to keep figuring out where some object is in it's method calls? There just has to be something better.


Can you clarify this fragment? What do you want to obtain? 10x

--
./pope
[the_mindstorm]
Dan Bizman
Ranch Hand

Joined: Feb 25, 2003
Posts: 387
Originally posted by Ali Pope:


Can you clarify this fragment? What do you want to obtain? 10x

--
./pope
[the_mindstorm]


I think I may have in my new post on this (since it's a separate question really). Please see this: http://www.coderanch.com/t/326672/java/java/Best-way-follow-notify-Object
 
 
subject: Help! Problem updating JProgressBar.
 
Similar Threads
Progress Bar refreshing!
Progress Bar refreshing!
updating JProgressBar
Vertical JProgressBar
JProgressBar and threads?