Today we will learn how to create Java Splash Screen With Progress Bar using Swing. The splash screen is nothing but a start-up loading screen of any software or application and usually contains a logo or other images.
A splash screen is beneficial when we have to load our application, and we know it will take some time. So in that duration, we can show a splash screen to our user so that the user will feel some process is going on. It gives a professional look to our application.
After going through this post, you will be able to make a splash screen in Java or Java loading screen for your application, and there are several ways by which you can make it more attractive.
I am going to use IntelliJ IDEA IDE for the coding part of my program. You can use any IDE like NetBeans or eclipse. So let’s start our tutorial and learn How To Create Java Splash Screen With Progress Bar using Swing step by step .
Also Read – Login Form in Java Swing
Java Splash Screen With Progress Bar | Java Loading Screen
Creating Display For Our Screen
step 1
- So in the very first step, we need to create a display for our screen.Simply create a new project and add a new class to your project(MainClass in this example).
- Now create main() method inside that class.
1 2 3 4 5 6 |
public class MainClass { ///Creating main() method public static void main(String[] args){ } } |
- Now add another class to your project (SplashScreenDemo in this example).
- Now to create a display screen for our splash screen, we have to create an object of JFrame class, and we will set some of its properties like its layout, visibility, location, size etc.
- Now we don’t want title bar in our JFrame so that it will give a feel of the splash screen.
- For this, we will call a method setUndecorated() using the object of JFrame and it takes a boolean value in its parameter so we will pass true in its parameter for removing the title bar.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import javax.swing.*; import java.awt.*; public class SplashScreenDemo { JFrame frame;//Creating object of JFrame SplashScreenDemo()//Creating constructor of the class { createGUI(); } public void createGUI(){ frame=new JFrame(); frame.getContentPane().setLayout(null);//setting layout to null frame.setUndecorated(true);//Turning off Title bar frame.setSize(600,400);//Setting size frame.setLocationRelativeTo(null);//Setting location to the center of screen frame.getContentPane().setBackground(Color.magenta);//setting background color frame.setVisible(true);//setting visibility } } |
- Now create an object of SplashScreenDemo inside main() method of MainClass.
1 2 3 4 5 6 |
public class MainClass { public static void main(String[] args){ new SplashScreenDemo();//Creating object of SplashScreenDemo class } } |
- Now Run your program.
- As you can see that we have built our screen without title bar.
- Now in next step, we will add an image to our screen.
Adding Image To Screen
Step 2
- To add an image to our Screen, we will Create an object of the JLabel class and in its constructor, we will add the specified image using the ImageIcon class.
- Now we will set the size of the image using the setSize() method and then we will add it to the screen using the add() method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import javax.swing.*; import java.awt.*; public class SplashScreenDemo { JFrame frame; JLabel image=new JLabel(new ImageIcon("book.png"));//Creating object of JLabel and adding image using the ImageIcon class SplashScreenDemo() { createGUI(); addImage(); } public void createGUI(){ frame=new JFrame(); frame.getContentPane().setLayout(null); frame.setUndecorated(true); frame.setSize(600,400); frame.setLocationRelativeTo(null); frame.getContentPane().setBackground(Color.magenta); frame.setVisible(true); } public void addImage(){ image.setSize(600,200);//Setting size of the image frame.add(image);//Adding image to the frame } } |
- Now Run your program.
- As you can see that we have added image to the screen.
- Now in next step, we will add some texts to our screen.
Adding Texts To Screen
Step 3
- To add texts to our screen, we will again create an object of the JLabel class and in its constructor we will pass the text that we want to add.
- Next, we will set some of its properties like its size and location, its font properties, its foreground color and then we will add it to the frame.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
import javax.swing.*; import java.awt.*; public class SplashScreenDemo { JFrame frame; JLabel image=new JLabel(new ImageIcon("book.png")); JLabel text=new JLabel("TUTORIALS FIELD");//Creating a JLabel object and adding text to it SplashScreenDemo() { createGUI(); addImage(); addText(); } public void createGUI(){ frame=new JFrame(); frame.getContentPane().setLayout(null); frame.setUndecorated(true); frame.setSize(600,400); frame.setLocationRelativeTo(null); frame.getContentPane().setBackground(Color.magenta); frame.setVisible(true); } public void addImage(){ image.setSize(600,200); frame.add(image); } public void addText() { text.setFont(new Font("arial",Font.BOLD,30));//Setting font size of text text.setBounds(170,220,600,40);//Setting size and location text.setForeground(Color.BLUE);//Setting foreground color frame.add(text);//adding text to the frame } } |
- Now run your program.
- As you can see that we have successfully added text to the screen.
- Now in next step, we will add a progress bar to our screen.
Adding Progress Bar To Screen
Step 4
- To add a progress bar to our Screen, we will create an object of the JProgressBar class and next we will set some of its properties.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
import javax.swing.*; import java.awt.*; public class SplashScreenDemo { JFrame frame; JLabel image=new JLabel(new ImageIcon("book.png")); JLabel text=new JLabel("TUTORIALS FIELD"); JProgressBar progressBar=new JProgressBar();//Creating an object of JProgressBar SplashScreenDemo() { createGUI(); addImage(); addText(); addProgressBar(); } public void createGUI(){ frame=new JFrame(); frame.getContentPane().setLayout(null); frame.setUndecorated(true); frame.setSize(600,400); frame.setLocationRelativeTo(null); frame.getContentPane().setBackground(Color.magenta); frame.setVisible(true); } public void addImage(){ image.setSize(600,200); frame.add(image); } public void addText() { text.setFont(new Font("arial",Font.BOLD,30)); text.setBounds(170,220,600,40); text.setForeground(Color.BLUE); frame.add(text); } public void addProgressBar(){ progressBar.setBounds(100,280,400,30);//Setting Location and size progressBar.setBorderPainted(true);//Setting border painted property progressBar.setStringPainted(true);//Setting String painted property progressBar.setBackground(Color.WHITE);//setting background color progressBar.setForeground(Color.BLACK);//setting foreground color progressBar.setValue(0);//setting progress bar current value frame.add(progressBar);//adding progress bar to frame } } |
- Now Run your program.
- As you can see that we have added progress bar to our screen.
- Now we want that if we run our program, then the progress bar should be running and it will give us an effect that some process is going on.
- For executing this we will create a loop from integer value 1 to 100 and we will set the value of progress bar to that integer value and we will also pause the execution of the thread for some milliseconds using the sleep() method so that progress bar will be paused for some milliseconds and then it will execute.
- For giving it a more professional look, I am also going to create a JLabel to the screen that will give us a message that how much process is being loaded.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
import javax.swing.*; import java.awt.*; public class SplashScreenDemo { JFrame frame; JLabel image=new JLabel(new ImageIcon("book.png")); JLabel text=new JLabel("TUTORIALS FIELD"); JProgressBar progressBar=new JProgressBar(); JLabel message=new JLabel();//Crating a JLabel for displaying the message SplashScreenDemo() { createGUI(); addImage(); addText(); addProgressBar(); addMessage(); runningPBar(); } public void createGUI(){ frame=new JFrame(); frame.getContentPane().setLayout(null); frame.setUndecorated(true); frame.setSize(600,400); frame.setLocationRelativeTo(null); frame.getContentPane().setBackground(Color.magenta); frame.setVisible(true); } public void addImage(){ image.setSize(600,200); frame.add(image); } public void addText() { text.setFont(new Font("arial",Font.BOLD,30)); text.setBounds(170,220,600,40); text.setForeground(Color.BLUE); frame.add(text); } public void addMessage() { message.setBounds(250,320,200,40);//Setting the size and location of the label message.setForeground(Color.black);//Setting foreground Color message.setFont(new Font("arial",Font.BOLD,15));//Setting font properties frame.add(message);//adding label to the frame } public void addProgressBar(){ progressBar.setBounds(100,280,400,30); progressBar.setBorderPainted(true); progressBar.setStringPainted(true); progressBar.setBackground(Color.WHITE); progressBar.setForeground(Color.BLACK); progressBar.setValue(0); frame.add(progressBar); } public void runningPBar(){ int i=0;//Creating an integer variable and intializing it to 0 while( i<=100) { try{ Thread.sleep(50);//Pausing execution for 50 milliseconds progressBar.setValue(i);//Setting value of Progress Bar message.setText("LOADING "+Integer.toString(i)+"%");//Setting text of the message JLabel i++; if(i==100) frame.dispose(); }catch(Exception e){ e.printStackTrace(); } } } } |
- Now Run your program.
- As we can see from the above animation, we have successfully created our splash screen.
- You can also download the source code of this Project. download link is given below..
Splash Screen Java Source Code
- Here you can download the source code of Java Swing Splash Screen with Progress Bar.
- The link of the file is given below.
So guys this was all from this post and if you have any doubts regarding this post then you can comment below. You can also check my previous post Registration Form in Java With Database Connectivity
People are also Reading…..
- Menu Driven Program in Java Using Switch Case
- How to Create Calculator in Java Swing
- How to Create Tic Tac Toe Game in Java
- How to Create Login Form in Java Swing
- Registration Form In Java with Database Connectivity
- How to Create Splash Screen In Java
- How to Create Mp3 Player in Java
- Java Button Click Event
Thank you dear. I like to connect with you.
You are welcome… You can see the contact us page to connect with me
Very Good and Clean Code
Loved the code.
Thanks
Thank you so much