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.

import java.awt.*;import java.awt.event.*;import com.sun.java.swing.*;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();  }}
Share the Post:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as