0

I have created two JFrames.
1. A Splash Screen
2. A Main Window

I need to display the Splash Screen 1st and then while the splash screen is displayed, the Main Windows object gets created. (it takes few seconds to create that object as it's a complex window.)

I have used a JXBusyLabel on the Splash Screen and it is set to busy by default.

This is my main method.

public static void main(String[] args) {

        SwingWorker sw = new SwingWorker() {

            @Override
            protected Boolean doInBackground() throws Exception {
                SplashScreen sp = new SplashScreen();
                sp.setVisible(true);
                return true;
            }
        };
        sw.execute();

        Thread t1 = new Thread() {

            public void run() {
                try {
                    /* some stuff here */
                    MainFrame mf = new MainFrame(); // (A)
                    mf.setVisible(true);
                } catch Exception ex) {
                    /* Logger */
                }
            }
        };

        t1.start();

}

When I run this, the splash screen appears and the busy label works fine for a while. And then it freezes, as the MainFrame gets created (in line (A) ).

I tried to use a normal thread to show the SplashScreen too. But the result was the same.

What am I doing wrong here??

Praneeth Peiris
  • 2,008
  • 20
  • 40
  • So you've just violated the single thread rule of Swing, twice...Take a read of [Concurrency In Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) for more details... – MadProgrammer Aug 23 '13 at 08:07
  • @MadProgrammer: Noo.. This SplashScreen is a JFrame I created. Not the Swing SplashScreen class. – Praneeth Peiris Aug 23 '13 at 08:17
  • That code would have being nice to see... – MadProgrammer Aug 23 '13 at 08:19
  • If you can, tell me how can I fix this. – Praneeth Peiris Aug 23 '13 at 08:21
  • 1
    The problem you are facing is the fact that Swing is going to try and move the rendering of both windows to the EDT, this means that while your splash screen is spinning away, the main frame will also be loaded into the same thread...causing the slow down/pause...You need to track down what is actually taking so long to load.... – MadProgrammer Aug 23 '13 at 08:24
  • The main frame is a huge MIS system, where many JPanels and other components get added to it. So the time taking to create that JFrame is not surprising. But when it loads, my Splash Screen freezes. Please, simple tell me how to run the splash screen in another thread. – Praneeth Peiris Aug 23 '13 at 08:27
  • You can't, Swing is a single threaded environment... – MadProgrammer Aug 23 '13 at 08:28
  • You mean, there is no way I can show the Splash Screen smoothly, while the main frame gets loaded in the background ?? – Praneeth Peiris Aug 23 '13 at 11:26
  • Yes. Once you call setVisible, both frames will be operating within the EDT – MadProgrammer Aug 23 '13 at 20:44
  • for emphasis, repeating @MadProgrammer's comment: _You need to track down what is actually taking so long to load_ Betting that it's _not_ the frame/component creation and configuration but some data loading. Separate out that bottleneck processing into a SwingWorker.doInBackground and create the ui only when done. BTW: the splash.setVisible in doInBackground is **wrong**! – kleopatra Aug 24 '13 at 14:03

0 Answers0