Splash Page Creation

Splash Page Creation

I would like to show a splash page and have it time out after approximately five seconds. How do I do that?

Presumably you would like to display a splash page while the application is loading, rather than freezing the application for five seconds. Any time you start talking about doing two things at once, you’ve got to think threads. You could always display the splash page only while the application is loading, in which case you would not need to use threads. Just show the splash page, load the application, and hide the splash page. But many applications load very quickly,and you would often like the user to see your splash page for a specified period of time, such as the five seconds in your question.

The Thread.sleep method allows you to introduce a delay into your program, suspending the current thread of execution forthe specified amount of time before resuming. There are two versions of Thread.sleep. The first accepts a single argument of type long that specifies the delay in milliseconds. The second accepts an additional integer argument that specifies an additional number of nanoseconds. Now that you know how to make a thread wait for a specified period of time, all you have to do is create a separate thread that shows the splash page, sleeps for five seconds (5000 milliseconds), and hides the splash page after waking up. The following listing demonstrates how to go about doing this using the Swing classes. I use a JDialog for the splash page because the native window manager will adorn it with a border and keep it placed on top of the parent window. JWindow instances have no borders and cannot take advantage of the native window system to keep them placed on top of a frame. Notice how I have to calculate the coordinates of the splash page in order to center it on the application window.

See also  How to Create and Deploy QR Codes Online: A Comprehensive Guide

import java.awt.*;import java.awt.event.*;import*;public class SplashPage {  JFrame __frame;  static class SplashPanel extends JPanel {    private Icon __splashIcon;    SplashPanel(Icon icon) {      __splashIcon = icon;      setPreferredSize(new Dimension(icon.getIconWidth(),				     icon.getIconHeight()));    }    public void paint(Graphics graphics) {      super.paint(graphics);      __splashIcon.paintIcon(this, graphics, 0, 0);    }  }    class SplashThread extends Thread {    private int __delay;    SplashThread(int delay) { __delay = delay; }    // Displays splash page on top of application frame, waits    // __delay milliseconds, and disposes of the splash page.    public void run() {      SplashPanel panel = new SplashPanel(new ImageIcon("splash.gif"));      JDialog splash;      Dimension size1, size2;      Point location;      location = __frame.getLocation();      size1    = __frame.getSize();      splash   = new JDialog(__frame);      splash.getContentPane().add(panel);      splash.pack();      size2     = splash.getSize();      splash.setLocation(location.x + (size1.width - size2.width)/2,			 location.y + (size1.height - size2.height)/2);      splash.setVisible(true);      // Wait five seconds, then dispose of splash.      try {	Thread.sleep(__delay);      } catch(InterruptedException e) {	// Don't do anything      }      splash.setVisible(false);      splash.dispose();    }  }    public SplashPage() {    WindowListener exitListener;    __frame = new JFrame("Splash Demo");    exitListener = new WindowAdapter() {      public void windowClosing(WindowEvent e) {        Window window = e.getWindow();        window.setVisible(false);        window.dispose();        System.exit(0);      }    };        __frame.addWindowListener(exitListener);    // any other application initialization goes here  }  public void loadApplication() {    Thread showSplash;    Dimension size;        size = Toolkit.getDefaultToolkit().getScreenSize();    __frame.setSize(640, 480);    // Center frame on screen.    __frame.setLocation((size.width - 640)/2, (size.height - 480)/2);    __frame.setVisible(true);    // Show the splash page for 5 seconds    showSplash = new SplashThread(5000);    showSplash.start();    // Continue loading your application here while the    // splash page is being displayed.  }  public static final void main(String[] args) {    SplashPage application;    application = new SplashPage();    application.loadApplication();  }}

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist