Java SWT Splash Window Example

Learn to create a splash window using the Java SWT toolkit for Windows applications that need a delay during application loading and initialization.

The splash window is an introductory screen that is shown to the user, by the time the application is loading on the background. Splash screens cover the entire screen or simply a rectangle near the center of the screen. Usually, a progress bar is also shown along with a splash window which indicates how much loading has been completed and/or how much is remaining.

In this tutorial, I am giving an example of such a splash screen. It also demonstrates the running progress bar. Once the progress bar reaches end/complete, the splash window closes down, and a new window appears which will be the main application window.

splash-screen

Below given code is only for demo purpose to help in building blocks. Please do not refer it for best practices.

1. Splash Window Code

Below is the source code for the splash window. The code is self-explanatory and should be easy to understand. If you still feel the problem, drop a comment.

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;
 
public class SplashWindow {
   
  private int splashPos = 0;
 
    private final int SPLASH_MAX = 100;
 
  public SplashWindow(Display display) 
  {
    final Image image = new Image(display,
        System.getProperty("user.dir")
                + CommonConstants.IMAGES_PATH
                + CommonConstants.FILE_NAME_SEPERATOR + "Splash.jpg");
 
        final Shell splash = new Shell(SWT.ON_TOP);
        final ProgressBar bar = new ProgressBar(splash, SWT.NONE);
        bar.setMaximum(SPLASH_MAX);
 
        Label label = new Label(splash, SWT.NONE);
        label.setImage(image);
 
        FormLayout layout = new FormLayout();
        splash.setLayout(layout);
 
        FormData labelData = new FormData();
        labelData.right = new FormAttachment(100, 0);
        labelData.bottom = new FormAttachment(100, 0);
        label.setLayoutData(labelData);
 
        FormData progressData = new FormData();
        progressData.left = new FormAttachment(0, -5);
        progressData.right = new FormAttachment(100, 0);
        progressData.bottom = new FormAttachment(100, 0);
        bar.setLayoutData(progressData);
        splash.pack();
 
        Rectangle splashRect = splash.getBounds();
        Rectangle displayRect = display.getBounds();
        int x = (displayRect.width - splashRect.width) / 2;
        int y = (displayRect.height - splashRect.height) / 2;
        splash.setLocation(x, y);
        splash.open();
 
        display.asyncExec(new Runnable()
        {
            public void run()
            {
 
                for(splashPos = 0; splashPos < SPLASH_MAX; splashPos++)
                {
                    try {
 
                        Thread.sleep(100);
                    }
                    catch(InterruptedException e) {
 
                        e.printStackTrace();
                    }
                    bar.setSelection(splashPos);
                }
                ApplicationLauncher.reportWindow.initWindow();
                splash.close();
                image.dispose(); 
            }
       });
 
        while(splashPos != SPLASH_MAX)
        {
            if(!display.readAndDispatch())
            {
                display.sleep();
            }
        }
    }
}

2. Main Application

This is not required in the demo. I used it to show the demonstration of another window opening when the splash window closes down.

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class ReportWindow
{
	private Display display;
	private Shell shell;

	public ReportWindow(Display display) {
		this.display = display;
	}

	public void initWindow() {
		shell = new Shell(display, SWT.CLOSE | SWT.MIN);
		shell.setText("Title");

		shell.open();
		shell.pack();

		Rectangle splashRect = shell.getBounds();
		Rectangle displayRect = display.getBounds();
		int x = (displayRect.width - splashRect.width) / 2;
		int y = (displayRect.height - splashRect.height) / 2;
		shell.setLocation(x, y);
	}

	public void destroyWindow() {
		shell.close();
		shell.dispose();
	}
}

3. Application Launcher

This code first launches the splash window. Splash the window run the progress bar and then at last open the application window.

import org.eclipse.swt.widgets.Display;

public class ApplicationLauncher
{
	//Creating static so that can access from splash window code
	//In production code, use event handling
	public static ReportWindow reportWindow;

    @SuppressWarnings("unused")
	public ApplicationLauncher()
    {
    	Display display = new Display();
    	reportWindow = new ReportWindow(display);
        SplashWindow splashWindow = new SplashWindow(display);
        while((Display.getCurrent().getShells().length != 0)
                 && !Display.getCurrent().getShells()[0].isDisposed())
        {
             if(!display.readAndDispatch())
             {
                 display.sleep();
             }
        }
    }

	public static void main(String[] args)
	{
		new ApplicationLauncher();
	}
}

For reference, these are constants used in the demo program.

public class CommonConstants {

    public static final String RESOURCE_PATH = "/resources";
    public static final String IMAGES_PATH = "/images";
    public static final String FILE_NAME_SEPERATOR = "/";
}

Let me know if any questions.

Happy Learning !!

Source Code download

Weekly Newsletter

Stay Up-to-Date with Our Weekly Updates. Right into Your Inbox.

Comments

Subscribe
Notify of
12 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.