wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Learning to use JProgressBar, having some problems. Need some help here. 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 "Learning to use JProgressBar, having some problems. Need some help here." Watch "Learning to use JProgressBar, having some problems. Need some help here." New topic
Author

Learning to use JProgressBar, having some problems. Need some help here.

Femi Alla
Ranch Hand

Joined: Jul 05, 2002
Posts: 79
Greetings everyone,
I'm thinking it would be nice to include a progress bar in an application I am developing to give the user an idea of what's going on behind the screen and how much's left to be done... I have never had to use the JProgressBar class, so I developed this application below to aid my understanding of the JProgressBar class without using the StringWorker class Sun's Java tutorial uses to explain how to use the JProgressBar class (truth is, I'm not sure I understood StringWorker totally) or some other Thread (how do I tie that Thread to this program? 'Haven't figured that out yet....).

The problem with this program is that when the start button is clicked to start the progress bar, the program blocks until the iteration in the program is completed and a full progress bar is displayed. What beats me is that I attempt to update the progress bar inside the actionPerformed method, which is inside the button listener class. But obviously, the JProgressBar (pb) is set when the only when the increasei method returns, that's why I'm getting this behaviour, right? How can I then get around this, without breaking the rule of allowing the realized component to be updated only inside the event dispaching thread?
Any useful suggestion would be greatly appreciated.
Thanks for reading up to this point.


SCJP
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Another example form the Swing book by Robinson and Vorobiev is basic progress bar example
The action listener for the start button creates an instance of the Thread class called "runme". Its run method contructs Runneable objects within a loop. These objects update the progress bar. They are passed to SwingUtilities.invokeLater(). This method will schedule the execution of such runneable in the drawing-event handling thread by placing the runneable in the event handling queue. This is necessary to ensure all drawing and event handling occurs in one thread.
Please read Threads and Swing from the Java Tutorial.


SCJP2. Please Indent your code using UBB Code
Femi Alla
Ranch Hand

Joined: Jul 05, 2002
Posts: 79
Thanks Jose,
It's quite clear to me now that it may not be possible to write a progress bar without an accompanying thread, somehow. Which was what I was trying to do....
Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Learning to use JProgressBar, having some problems. Need some help here.
 
Similar Threads
calling java class
JProgressBar in Napkin look and feel is not working
JTable
How to handle events in java
Stack OverFlow Error