Java SWT – Create Splash Window Example

Splash window is introductory screen which is shown to the user, by the time application is loading on 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 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 splash screen. It also demonstrate the running progress bar. Once progress bar reaches to end/complete, splash window close down and a new window appears which will be main application window.

Sections in this post:

1) Splash window 
2) Main application window
3) Application demo launcher
4) Sourcecode download

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 sourcecode for splash window. Code is self explanatory and should be easy to understand. If you still feel problem, drop a comment.

package com.howtodoinjava.splash.demo;

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();
            }
        }
    }
}
&#91;/java&#93;

<h3><strong>2) Main application window code</strong></h3>

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


package com.howtodoinjava.splash.demo;

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 demo launcher code

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

package com.howtodoinjava.splash.demo;

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 demo program.

package com.howtodoinjava.splash.demo;

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

4) Sourcecode download

To download the sourcecode of above splash screen demo, follow the below link.

Let me know if any question.

Happy Learning !!

Was this post helpful?

Join 7000+ Awesome Developers

Get the latest updates from industry, awesome resources, blog updates and much more.

* We do not spam !!

12 thoughts on “Java SWT – Create Splash Window Example”

  1. I have the same problem, I am running eclipse in Mac BigSurf OS and I attach swt.jar and org.eclipse.swt.win32.win32.x86_3.1.0.jar under external JAR. I got an error. I had a feeling that org.eclipse.swt.win32.win32.x86_3.1.0.jar was not the right JAR as my Display class was still undefined. I google and got org.eclipse.swt-3.1.jar , and I remove org.eclipse.swt.win32.win32.x86_3.1.0.jar and add org.eclipse.swt-3.1.jar as the external JAR and run it cleared the Display class error but it gave the same error message :

    Exception in thread "main" java.lang.UnsatisfiedLinkError: no swt-win32-3236 in java.library.path: 
    /Users/epai/Library/Java/Extensions:/Library/Java/Extensions:
    /Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.
        at java.base/java.lang.ClassLoader.loadLibrary(ClassLoader.java:2423)
        at java.base/java.lang.Runtime.loadLibrary0(Runtime.java:808)
        at java.base/java.lang.System.loadLibrary(System.java:1893)
        at org.eclipse.swt.internal.Library.loadLibrary(Library.java:123)
        at org.eclipse.swt.internal.win32.OS.(OS.java:18)
        at org.eclipse.swt.widgets.Display.(Display.java:125)
        at ApplicationLauncher.(ApplicationLauncher.java:12)
        at ApplicationLauncher.main(ApplicationLauncher.java:27)

    These org.eclipse.. libraries are still used in Eclipse, I just. wonder? I appreciate it if you could advise me to solve …

    Reply
  2. Exception in thread “main” java.lang.UnsatisfiedLinkError: no swt-pi-gtk-3139 in java.library.path
    at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1886)
    at java.lang.Runtime.loadLibrary0(Runtime.java:849)
    at java.lang.System.loadLibrary(System.java:1088)
    at org.eclipse.swt.internal.Library.loadLibrary(Library.java:123)
    at org.eclipse.swt.internal.gtk.OS.(OS.java:19)
    at org.eclipse.swt.internal.Converter.wcsToMbcs(Converter.java:63)
    at org.eclipse.swt.internal.Converter.wcsToMbcs(Converter.java:54)
    at org.eclipse.swt.widgets.Display.(Display.java:122)
    at VettiVela.ApplicationLauncher.(ApplicationLauncher.java:14)
    at VettiVela.ApplicationLauncher.main(ApplicationLauncher.java:29)

    Reply
  3. Hi Lokesh,
    present im working on swt but i dont no indetail of pulgin.xml file of each tag what we required in projects.So please tell me it is very urgent to me.

    Reply
  4. I am beginner in Java Programming and just started my career in Java, i have done this program in core java and ran that code in traditional way.
    pls give steps to run your source code

    Reply
  5. how to run it?iam getting following error

    Exception in thread “main” java.lang.UnsatisfiedLinkError: no swt-win32-3138 in java.library.path
    at java.lang.ClassLoader.loadLibrary(Unknown Source)
    at java.lang.Runtime.loadLibrary0(Unknown Source)
    at java.lang.System.loadLibrary(Unknown Source)
    at org.eclipse.swt.internal.Library.loadLibrary(Library.java:123)
    at org.eclipse.swt.internal.win32.OS.(OS.java:18)
    at org.eclipse.swt.widgets.Display.(Display.java:125)
    at com.howtodoinjava.splash.demo.ApplicationLauncher.(ApplicationLauncher.java:14)
    at com.howtodoinjava.splash.demo.ApplicationLauncher.main(ApplicationLauncher.java:29)

    Reply

Leave a Comment

HowToDoInJava

A blog about Java and related technologies, the best practices, algorithms, and interview questions.