How to display Splash Screen in Java



We all know the importance of user interface in any commercial as well as non commercial software. Today while working on one of my project I felt that the loading time of my application is quite longer than usual.

The reasons behind  longer loading time are the loading of configuration file and downloading of resources from web. Here splash screen is the requirement for this situation.

 Although there are many ways to implement splash screen in java but I prefer the built in API for splash screen. Let's see how to use this native Splash screen API for your application.

Java SE 6 provides a solution that allows the application to display the splash screen much earlier, even before the virtual machine starts. A Java application launcher is able to decode an image and display it in a simple non-decorated window.

Below is the sample Java Swing application that do some time consuming task before initializing UI.

Copy and save below code in file  MySplashScreenDemo.java

 package com.decryptoniter.splash;  
 import java.awt.FlowLayout;  
 import javax.swing.*;  
 public class MySplashScreenDemo {  
  MySplashScreenDemo() {  
  // some time consuming task before UI initialization  
  someTask();  
  // UI will initialize now   
  initUI();  
  }  
  void someTask() {  
  System.out.println("Some time consuming task is executed");  
  try {  
   Thread.sleep(10000);  
  } catch (Exception e) {  
   e.printStackTrace();  
  }  
  }  
  void initUI() {  
  JFrame frame = new JFrame("Splash Screen Demo");  
  JPanel panel = new JPanel();  
  panel.setLayout(new FlowLayout());  
  JLabel label = new JLabel("www.isumitjha.com");  
  panel.add(label);  
  frame.add(panel);  
  frame.setSize(300, 300);  
  frame.setLocationRelativeTo(null);  
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  frame.setVisible(true);  
  System.out.println("UI initialized");  
  }  
  public static void main(String s[]) {  
  MySplashScreenDemo ss = new MySplashScreenDemo();  
  }  
 }  

Now compile MySplashScreenDemo.java:
javac -d . MySplashScreenDemo.java

Run this application: java com.decryptoniter.splash.MySplashScreenDemo

You will notice that it takes 15 seconds to load the UI after running the application. Now to display splash screen while UI is not loaded we can use java command line argument.

Display a Splash Screen using the Command-Line Argument 

To display a splash screen from the command line use the -splash: command-line argument. This argument is a Java application launcher option that displays a splash screen:

java -splash:<file name> <class name>

For our application use. java -splash:loading.gif  com.decryptoniter.splash.MySplashScreenDemo

Now you can see the splash screen as soon as application is run while UI is not loaded.

Note: The splash screen can display any gif, png, or jpeg image, with transparency, translucency, and animation.

14 comments:

  1. very good introduction night geekowl
    plus the information

    ReplyDelete
    Replies
    1. I am glad that you like the article and intro too :p

      Delete
  2. Really very interesting and useful article of the developers like me.

    Java Training in Chennai

    ReplyDelete
  3. Nice it seems to be good post... It will get readers engagement on the article since readers engagement plays an vital role in every blog.. i am expecting more updated posts from your hands.Thanks for sharing. Anybody want to learn
    Core Java Online Training

    ReplyDelete
  4. Your information about Java is useful for me to know more technical information. Really very informative post you shared here. Keep sharing this type of informative blog. If anyone wants to become a Java professional learn Java Training in Bangalore. Nowadays Java has tons of job opportunities for all professionals...Big Data Hadoop Training in Bangalore | Data Science Training in Bangalore

    ReplyDelete
  5. It's interesting that many of the bloggers to helped clarify a few things for me as well as giving.Most of ideas can be nice content.The people to give them a good shake to get your point and across the command.



    Java Training in BTM Layout


    Java Training in Marathahalli

    ReplyDelete
  6. I simply wanted to thank you so much again. I am not sure the things that I might have gone through without the type of hints revealed by you regarding that situation.

    Java Training in Bangalore|

    ReplyDelete
  7. Thank you a lot for providing individuals with a very spectacular possibility to read critical reviews from this site.


    http://www.traininginmarathahalli.in/

    ReplyDelete
  8. I feel really happy to have seen your webpage and look forward to so
    many more entertaining times reading here. Thanks once more for all
    the details.


    Selenium Training in Chennai

    ReplyDelete
  9. Needed to compose you a very little word to thank you yet again regarding the nice suggestions you’ve contributed here.

    Data Science Training in Bangalore

    Datascience Training in Chennai

    ReplyDelete
  10. I really enjoy your post.I will be waiting for your another blog & i want more Java Training in Chennai
    Oracle Training in Chennai

    ReplyDelete
  11. • Nice and good article. It is very useful for me to learn and understand easily. Thanks for sharing your valuable information and time. Please keep updating. Power Bi Online Training Hyderabad

    ReplyDelete
  12. I believe there are many more pleasurable opportunities ahead for individuals that looked at your site.

    Selenium Training in Bangalore

    Selenium Training in BTM

    Selenium Training in Rajaji Nagar

    ReplyDelete
  13. Those guidelines additionally worked to become a good way to recognize that other people online have the identical fervor like mine to grasp great deal more around this condition. Selenium Training In Bangalore | Best Selenium Training in Bangalore

    ReplyDelete