-1

i get this source code for creating splash screen and thread management in Java. But i don't know how to implement it.

public class SplashWindow extends JWindow {
    public SplashWindow(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();
    }
}

I try to implement like this : ... public static void main(String[] args) { JFrame frame = new JFrame(); frame.setSize(500, 500);

        SplashWindow window = new SplashWindow("splash-scren.jpg", frame, 1000);
    }
...

But nothing to show. Please help me, thank you :)

fanjavaid
  • 1,676
  • 8
  • 34
  • 65
  • 5
    Don't reinvent the wheel; Java already has a built-in implementation of a [splash screen](http://docs.oracle.com/javase/tutorial/uiswing/misc/splashscreen.html). Note: This splash screen is shown *before* the JVM starts. After that you can change the image / etc with the [`SplashScreen`](http://docs.oracle.com/javase/7/docs/api/java/awt/SplashScreen.html) class. – Jeffrey Mar 31 '13 at 03:17
  • See also [*Java Web Start*](http://stackoverflow.com/tags/java-web-start/info) and this [Q&A](http://stackoverflow.com/q/11399971/230513).. – trashgod Mar 31 '13 at 06:01

1 Answers1

1

Dont put :

"setVisible(true);"

In the constructor , after

SplashWindow window = new SplashWindow("splash-scren.jpg", frame, 1000);

write:

window.setVisible(true);
Aviram Fireberger
  • 3,910
  • 5
  • 50
  • 69