How to add a progress bar in Java Swing

In this tutorial, we are going to create a progress bar using Java Swing. Java Swing is an integral component of the Java Foundation Classes(JFC). It is a library of graphical user interface components which is used to create window-based applications in Java.

Code: add a progress bar in Java Swing

import javax.swing.*;
import java.awt.*;

public class ProgressBar{
    public static void main(String[] args) {
        JFrame frame = new JFrame("Progress Bar");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300,150);
        frame.setLocationRelativeTo(null);
        frame.setLayout(new FlowLayout());

        JProgressBar progressBar = new JProgressBar(0, 100);
        progressBar.setValue(0);
        progressBar.setStringPainted(true);

        frame.add(progressBar);
        frame.setVisible(true);

        int progress = 0;
        while (progress <= 100) {
            progressBar.setValue(progress);
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            progress++;
        }
    }
}

Output

Here is the output of the above code:

progress bar in java swing

Explanation

In the above Java code we are creating a progress bar using the Swing library.

  1. First we start by importing the necessary classes from the javax.swing and java.awt packages.
  2. Then, we define a class named ProgressBar inside which we declare the main method. In the main method, we create a JFrame object named frame with the title Progress Bar.
    We use the setDefaultCloseOperation() method to exit the program when the frame is closed.
  3. Next, we use the setSize() method to set the size of the frame to 300×150 pixels and used the setLocationRelativeTo(Null) method to center it on the screen.
  4. After that, we create a JProgressBar object named progressBar with a minimum value of 0 and a maximum value of 100.
    Then we set the initial value of the progress bar to 0 and call the setStringPainted(true) method to display the progress value as a string on the progress bar.
  5. We add the progress bar to the frame using the add() method and set the frame to be visible using the setVisible() method.
    Then we initialize a variable named progress and set it to 0.
  6. Next, we use a while loop to update the value of the progress bar from 0 to 100.
    Inside the loop, we set the value of the progress bar to the current value of the progress variable using the setValue() method.
    The program then pauses for 100 milliseconds using the Thread.sleep() method.
  7. After the pause, the value of the progress variable is incremented by 1. The loop continues until the value of the progress variable reaches 100.

Thank you for learning from CodeSpeedy. If you have any doubts or questions then feel free to ask them in the comments section below.

You can also read:

Leave a Reply

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