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.
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
Joined: Jul 05, 2002
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