简体   繁体   中英

Displaying JWindow in the event dispatching thread

What I am trying to do is have a small splash screen appear while my program is loading something. This is what I have:

SplashScreen.showSplashScreen();
// Do stuff that takes time.
SplashScreen.hideSplashScreen();

All the showSplashScreen() method does is create a new JWindow in the middle of the screen and make it visible.

Now this code is called from the event dispatching thread, so when the showSplashScreen() method is called, I don't get to see the JWindow until the thread has finished, which by then, I don't need the window anymore. What would be the best way to go about showing this splash screen while I was waiting?

Not sure if this is the "best way", but a mechanism I've used before is to do your initialisation on a thread other than the EDT, but show your splash screen using SwingUtilities.invokeAndWait . That way, you'll at least get to see the splash screen even if your initialisation is quick (if that's what you want to happen).

So on your init thread, you go:

SwingUtilities.invokeAndWait( /* Runnable to show splash screen */ );

// Do stuff that takes time.

SwingUtilities.invokeLater( /* Hide splash screen, display main GUI */ );

在1.6中引入了一个java.awt.SplashScreen类,尝试使用它?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM