I am developing a big Swing application with a lot of components that are loaded at the launch of the application. The process of loading the components, setting their attributes and adding them to panels takes a few seconds.
The applications UI freezes during the loading of the application. I would like to show a splash loading screen for the time of loading the whole UI, but the splash also freezes.
If I move the UI construction to another Thread, this would be the EDT Threading violation.
What is the correct way to load such Swing interface without freezing the UI?
I have seen existing answers on this topic but they cover the case where external tasks not related to Swing are blocking the UI, but here, the construction of the complex UI itself is blocking the UI refreshing (splash screen progress bar).
There is two possibilities to solve this problem.
SwingUtilities.invokeLater
. Each invokeLater
triggers repainting, so your GUI can be refreshed. This should be the preferred way.When you have problems on splitting your appllication into small independent chunks, you can "misaply" the Foxtrot lib to provide synchron breaks for UI repainting
public static void sleepNonBlocking(long millis) {
Worker.post(new Job() {
@Override
public Object run() {
try {
Thread.sleep(millis);
} catch (Exception e) {
// nothing
}
return null;
}
});
}
You need to call this method on some places in your application to force repainitng.