ars265 ars265 - 3 days ago 5
Java Question

Java Swing Threading with Updatable JProgressBar

First off I've been working with Java's Concurrency package quite a bit lately but I have found an issue that I am stuck on. I want to have and Application and the Application can have a

SplashScreen
with a status bar and the loading of other data. So I decided to use
SwingUtilities.invokeAndWait( call the splash component here )
. The
SplashScreen
then appears with a
JProgressBar
and runs a group of threads. But I can't seem to get a good handle on things. I've looked over
SwingWorker
and tried using it for this purpose but the thread just returns. Here is a bit of pseudo code. and the points I'm trying to achieve.


  • Have an Application that has a
    SplashScreen
    that pauses while loading info

  • Be able to run multiple threads under the
    SplashScreen

  • Have the progress bar of the
    SplashScreen
    Update-able yet not exit until all threads are done.



Launching splash screen

try {
SwingUtilities.invokeAndWait( SplashScreen );
} catch (InterruptedException e) {
} catch (InvocationTargetException e) { }


Splash screen construction

SplashScreen extends JFrame implements Runnable{

public void run() {
//run threads
//while updating status bar
}
}


I have tried many things including
SwingWorkers
, Threads using CountDownLatch's, and others. The CountDownLatch's actually worked in the manner I wanted to do the processing but I was unable to update the GUI. When using the
SwingWorkers
either the
invokeAndWait
was basically nullified (which is their purpose) or it wouldn't update the GUI still even when using a
PropertyChangedListener
. If someone else has a couple ideas it would be great to hear them. Thanks in advance.

I actually got ready to post better code to help out and found my solution. I thank you for all who helped.

Answer

No need to call the frame inside invokeAndWait but you should update progress bar state like this.

try {
   SwingUtilities.invokeAndWait( new Runnable() {
     public void run() {
//update state of the progress bar here
     }
   });
 } catch (InterruptedException e) {
 } catch (InvocationTargetException e) { }
Comments