> I know that Im a newbie at this, but should I be creating a class that
> holds my JProgressBar and implements Runnable?
[quoted text clipped - 21 lines]
> }
> }
No, leave the JProgressBar alone, it is fine how it is. If you move it
to a different thread it will not be on the EDT i mentioned in my last
post, and so will violate the rules of Swing.
You need to move your lengthy calculation to another thread, that is put
it within a runnable, it will then allow the JProgressBar to update.
i.e.
in the method that creates the JProgressBar and calls the method to
perform the really long conversation you may have something that looks
like this
JProgressBar progressBar = ...
...
someObject.performLengthyComputation();
...
Instead you want to create a new thread for the lengthy computation. It
might look something like this
JProgressBar progressBar = ...
....
new Runnable() {
public void run() {
someObject.performLengthyComputation();
swingUtilities.invokeLater(new Runnable() {
public void run() {
//stop JProgressBar
//using the invokeLater() method ensures that the JProgressBar
//is accessed from the EDT only
});
}.run();
...
[Note not tested or compiled]
I saw something on another thread that would indicate that you can avoid
the second runnable to update the JProgressBar
JProgressBar progressBar = ...
....
try {
new Runnable() {
public void run() {
someObject.performLengthyComputation();
//don't stop JProgressBar
}.run();
} finally {
//stop JProgressBar
}
[Again not tested or compiled]
There will be far more elegant ways of doing this, I'm just trying to
put the point across.
I would recommed that you look at the SwingWorker class. It is designed
to do exactly what you are trying to do.
http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html
James
tiewknvc9 - 23 Feb 2006 16:09 GMT
very helpful. I read it all. Thanks!