HowToDoInJava

  • Java 8
  • Regex
  • Concurrency
  • Best Practices
  • Spring Boot
  • JUnit5
  • Interview Questions
  • Dark Mode

Java SWT – Create Splash Window Example

By Lokesh Gupta | Filed Under: SWT

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.


Sourcecode download

Let me know if any question.

Happy Learning !!

TwitterFacebookLinkedinRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

10
Leave a Reply

This comment form is under antispam protection
4 Comment threads
6 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
5 Comment authors
This comment form is under antispam protection
  Subscribe  
newest oldest most voted
Notify of
Premkumar

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)

Vote Up0Vote Down  Reply
4 years ago
Lokesh Gupta

Strange !! Did you added any additional jar file or 3rd party library?

Vote Up0Vote Down  Reply
4 years ago
subbareddy

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.

Vote Up0Vote Down  Reply
6 years ago
Pawan

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

Vote Up0Vote Down  Reply
6 years ago
Lokesh Gupta

Pawan, please download the sourcecode and import the project in eclipse. Now use eclipse runner to execute ApplicationLauncher.java

Vote Up0Vote Down  Reply
6 years ago
Pawan

thanks
got that and now want to create stand alone desktop application of your source code…………

Vote Up0Vote Down  Reply
6 years ago
Ramachandra

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)

Vote Up0Vote Down  Reply
6 years ago
Lokesh Gupta

Try attaching swt.jar as external jar file.

Vote Up0Vote Down  Reply
6 years ago
Ramachandra

i have added the swt.jar in the buildpath,is there any arguments i have to add during the compilation?

Vote Up0Vote Down  Reply
6 years ago
Lokesh Gupta

In eclipse, you can ajj jar in classpath directly (lib folder) or you can add external jars (browse complete path in file system). Try second way. It seems absurd, but it works.

Vote Up0Vote Down  Reply
6 years ago

Search Tutorials

Popular Tutorials

  • Java 8 Tutorial
  • Core Java Tutorial
  • Collections in Java
  • Java Concurrency
  • Spring Boot Tutorial
  • Spring AOP Tutorial
  • Spring MVC Tutorial
  • Spring Security Tutorial
  • Hibernate Tutorial
  • Python Tutorial
  • Jersey Tutorial
  • Maven Tutorial
  • Log4j Tutorial
  • Regex Tutorial

Meta Links

  • Advertise
  • Contact Us
  • Privacy policy
  • About Me

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2016 · HowToDoInjava.com · All Rights Reserved. | Sitemap

wpDiscuz