Creating a Splash Screen in Java using NetBeans IDE

This is one of those tutorials that is based on user request. Many readers(ok…few) ask me that we want to show a loading screen when our app start just to give it more realistic and professional look. So, here’s a guide that will help you to develop a simple splash screen for your Java Application in NetBeans IDE.

Note: This article shows how to display a loading image at the start of your app, just to pretend that your app is actually loading up some resources. But if you want to create a real splash screen, follow up this article, instead.

loading.gif

Before Starting

If you are using an old project for this tutorial, make sure that your JFrameForms are not be placed under a [default package] and the package should contain a main class. Main class has a icon like this . If it does not contain a main class, you can create one by right clicking your package New>Other and then select Java Main Class and complete the procedure.
If you are creating a new Project, make sure that you have “Create Main Class” checked.

 

Let’s Get Started

Step-1 Create a new JFrame Form, if you don’t have it already.

Step-2 Find/Design a loading image that you want to show when your Application starts. An animated one will look cool!!

Step-3 Now, drag and the drop the image at YourProjectName>Source Packages>YourPackageName. Your Project should look like this now…

 

Step-4 Now go to Files (CTRL + 2), open the manifest.mf file and below the line which states your Manifest-Version type the following:

SplashScreen-Image: your_package_name/your_loading_image_name.jpg/png/gif

Note: There are no line breaks in the above line. It should be typed in single continuous line like other declarations in the manifest.

At this point if we run our app(//don’t try it we still have something missing, this is just to explain), we will see the image for only a split second. Now what we have to do is make our thread sleep for few seconds so that the processing is stopped and the image is visible for longer time.

Step-5 Open your main class and type the following method to make thread sleep for few seconds.

private static void sleepThread() {
        try
            {
                Thread.sleep(5000);
            }
            catch (InterruptedException ex)
            {
                // Do something, if there is a exception
                System.out.println(ex.toString());
            }
    }

The parameter 5000 above is in milliseconds i.e 5 seconds. Change it according to your need.

Step-6 Call the above method in the main constructor and paste the runnable of your form below it. Here’s the full main class to make it clear…

package demosplash;

/**
 *
 * @author Rohan
 */
public class DemoSplash {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        sleepThread();
       
         java.awt.EventQueue.invokeLater(new Runnable() {

            @Override

            public void run() {
                new Welcome().setVisible(true);
            }
        });
    }
   
     private static void sleepThread() {
        try
            {
                Thread.sleep(5000);
            }
            catch (InterruptedException ex)
            {
                // Do something, if there is a exception
                System.out.println(ex.toString());
            }
    }
}

Instead Welcome write your Form’s name.

Step-7 Now we are ready with the coding and we just need a simple tweak to see our splash screen when we run it from a IDE. So, right click your project select properties>Run and type the following code in the VM Options(including the minus sign) -splash:src/your_package_name/loading.gif.

Now, right click your project and select run to see the magic.

Step-8 (Optional) If your app doesn’t run on a full screen, you would notice that your splash screen appears in the middle of the screen and as soon as it finishes the app’s flashes at top left corner and that doesn’t give a professional look. So if you want that your app should also be place in the center of the screen then, Right Click your form>Events>windowOpened and paste the following code-

this.setLocationRelativeTo(null);

If this post helped you, then please give a minute and share it with your friends so that they could also be helped. Thanks for giving it a read!!

17 thoughts on “Creating a Splash Screen in Java using NetBeans IDE”

  1. I know this is not actual splash, but this will help beginners to get basic idea and allow them to just give a cool look to their app.
    I thought to make video tutorial for making a real splash screen because explaining those typical methods in a blog post would be difficult.
    Thanks for giving it a read!

  2. Hey man!!! Great tutorial it helped me a lot! But i have a problem… The splash only works inside the IDE… When i clean and build and then run the .jar file it doesn't work!!! any help?

  3. Oh my word!!! Its working perfectly 😀
    Thank you soo much!!!
    I'll be sure to recommend your blog to friends!!!
    Keep up the awesome work!

  4. Hi…
    How can I place this splash screen to be displayed while switching between two jFrames. when I run my project. I usually starts automatically with the my first frame in order.

  5. My brother did the right !!! I am using NetBeans 8.0.

    and always gives this error: Existing manifest C: Users cpflcosf Documents NetBeansProjects SplashDemo build null829639409 is invalid

Leave a Reply to sibin xavier Cancel Reply

Your email address will not be published. Required fields are marked *