I have a program, that takes a long time to load. Because of this, I wanted to develop a splash screen that can provide feedback to the user on what is being loaded. A simple JFrame with an image, label and JProgressBar.
I have been experimenting and the best results I’ve had are doing this in my main():
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
new SplashScreen();
}
});
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
//Code to start system
new MainFrame();
//.. etc
}
});
Both SplashScreen and MainFrame are classes extending JFrame. I am also using Substance as a Library.
SplashScreen’s constructor adds a JLabel and JProgressBar to itself, packs and sets visible.
The JProgressBar is setIndeterminate(true);
When I run my program, my SplashScreen is displayed but the ProgressBar is locked, it doesn’t move, not until the rest of the program has started does it start moving as expected.
What am I missing here? All searching I’ve done doesn’t seem to mention this problem and most “custom splash screen” implementations go about it a very similar way to myself.
Other answers have covered most of this but in brief your problem is that you are running the “Code to start system” in the Swing event dispatch thread. All GUI-related code (including component creation) must be run on the EDT but all other code should not be run on the EDT. Try changing your program to do this: