Sunday, July 22, 2012

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!!

18 comments:

  1. No offense, but this will not make an actual splash screen, it will only start your application 5 seconds later. For instance, if your application needs 3 seconds to start, it will show you nothing for 3 seconds, then it will show you splash screen for 5 seconds, and THEN - 8 seconds after running it will start.
    There is an easy way to make real splash screen in JavaFX (you will see splash screen either 3, 8 or 20 seconds, depending on how much your application needs to start, and it will start on very moment you run it). Check it out :)

    ReplyDelete
  2. 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!

    ReplyDelete
  3. Ya Rohan please give a real splash screen tutorial, this is just show off ! :P

    ReplyDelete
  4. It's really a nice and helpful piece of information. I'm glad that you simply shared this helpful information with us.
    Please keep us up to date like this. Thank you for sharing.
    Stop by my web site :: search engine

    ReplyDelete
  5. Helped me out. Thanks a lot.

    ReplyDelete
  6. THANKS YOU!!! its awesome , thanks for share this..

    ReplyDelete
  7. 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?

    ReplyDelete
    Replies
    1. thank you!
      Have you registered your image in the manifest properly(step-4)?
      Keep in mind one thing, there is no line break in that line.
      It should be typed in a single line!

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

    ReplyDelete
  9. hey man i want to tlk to u bcoz i have many problems about splash form.so give me your no.

    ReplyDelete
  10. There's a problem the main class couldn't find the splash:src/my_package_name/loading.gif.......

    ReplyDelete
    Replies
    1. is my_package_name really your package name?

      Delete
  11. hi........good tutorial.But i want to do this without NeatBeans i.e with notepad,Then what should i do?

    ReplyDelete
  12. copy and paste it in notepad :D

    ReplyDelete
  13. 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.

    ReplyDelete
  14. Thank you.
    I being a beginner in NetBeans, your examples are very helpful to new people.

    ReplyDelete
  15. .gif is not working :( why its not working i cant get it .png and.jpeg is working

    ReplyDelete