Splash Screen before program begins

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AlarV
    New Member
    • Dec 2008
    • 37

    Splash Screen before program begins

    Hey everyone!
    I've been trying to create a Splash Screen in my Java program. I've managed to make the splash screen show itself, with the program. I now want to make it show first, and then show my main program. As in every program nowadays..

    Here is my code:

    Code:
    /*
    
    
    package ****;
    
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.Frame;
    import java.awt.Toolkit;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JWindow;
    import javax.swing.SwingUtilities;
    
    
    class SplashWindow1 extends JWindow
    {
    
    
        public SplashWindow1(String filename, Frame f,int waitTime)
        {
            super(f);
            JLabel l = new JLabel(new ImageIcon(filename));
            getContentPane().add(l, BorderLayout.CENTER);
            pack();
            Dimension screenSize =
              Toolkit.getDefaultToolkit().getScreenSize();
            Dimension labelSize = l.getPreferredSize();
            setLocation(screenSize.width/2 - (labelSize.width/2),
                        screenSize.height/2 - (labelSize.height/2));
            addMouseListener(new MouseAdapter()
                {
                    public void mousePressed(MouseEvent e)
                    {
                        setVisible(false);
                        dispose();
                    }
                });
            final int pause = waitTime;
            final Runnable closerRunner = new Runnable()
                {
                    public void run()
                    {
                        setVisible(false);
                        dispose();
                    }
                };
            Runnable waitRunner = new Runnable()
                {
                    public void run()
                    {
                        try
                            {
                                Thread.sleep(pause);
                                SwingUtilities.invokeAndWait(closerRunner);
                            }
                        catch(Exception e)
                            {
                                e.printStackTrace();
                                // can catch InvocationTargetException
                                // can catch InterruptedException
                            }
                    }
                };
                
            setVisible(true);
            Thread splashThread = new Thread(waitRunner, "SplashThread");
            splashThread.start();
        }
    }
    and in the Main class I call the SplashScreen like this:

    Code:
    JFrame myframe = new Main();
    SplashWindow1 a = new SplashWindow1("C:/*********/splash.gif",myframe,4000);
    With this code it runs with the program, so the splash screen shows and myframe is visible too and I can start working.

    I want the splash screen to show first, load stuff etc. and then show myframe!

    I thought things like

    Code:
    Frame f=new JFrame();
                        SplashWindow1 a = new SplashWindow1("C:/*****/splash.gif",f,4000);
                        while(a.isVisible()) try {
                            Thread.sleep(100);
                        } catch (InterruptedException ex) {
                            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                        }
    but it doesn't work, the JWindow of the splash screen is like stuck, and after the 4 seconds my main program shows.


    Any ideas??

    Thanks in advance!
    Alex
Working...