Table of contents
1.
Introduction
2.
Constructors of JProgressBar 
3.
Fields of JProgressBar
4.
Methods of JProgressBar
5.
Syntax of JProgressBar
6.
Methods Inherited 
7.
Example of JProgressBar
8.
Frequently Asked Questions
8.1.
How to change the color of JProgressBar?
8.2.
What is the work of the getMaximum() method in JProgressBar?
8.3.
Which constructor is used to create a JProgressBar with defined orientation?
8.4.
Which constructor is used to create the JProgressBar with maximum and minimum values along with its orientation? 
8.5.
Which method returns the maximum value of JProgressBar?
9.
Conclusion
Last Updated: Mar 27, 2024
Easy

JProgressBar

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

JProgressBar is a class of JavaSwing that displays the progress of any particular task.

JProgressBar shows the completion of any particular task by filling the progress bar according to the completion of the task, and it also displays the progress in the form of a percentage. It also can display some text.

Progress in the progress bar is monitored by the API of Swing, which consists of three classes. JComponent is one of the subclasses of JProgressBar, which is the graphical component that displays the progress of the task. 

Constructors of JProgressBar 

A constructor is a function that initialises the objects of a class. In simple words, we can say that a constructor is a function that constructs all the features of a class. There are some constructors in JprogressBar, which are as follows:

  • JProgressBar(): It is the primary constructor of JProgressBar. It constructs a plain progress bar without having the ability to display any text. For example “jpb=new JProgressBar();”

 

  • JProgressBar(int orientation): This constructor of JProgressBar constructs the progress bar with the mentioned orientation in its object. If VERTICAL is mentioned in the orientation section of the object, then it will create a vertical progress bar. HORIZONTAL mentioned in the orientation section of the object leads to creating a progress bar with horizontal orientation.For example  “jpb = new JProgressBar(SwingConstants.VERTICAL);” , “jpb = new JProgressBar(SwingConstants.VERTICAL);”

 

  • JProgressBar(int min, int max): This constructor of JProgressBar constructs the progress bar along with the mentioned minimum and maximum values. For example “jpb=new JProgressBar(0,100);”   

 

  • JProgressBar(int orientation, int min, int max): This constructor of JProgressBar constructs the progress bar along with the mentioned minimum and maximum value and the object's defined orientation. In SwingConstants, if VERTICAL is declared, then it will construct a vertical progress bar, and if HORIZONTAL is declared, then it will construct a horizontal progress bar. For example  “jpb = new JProgressBar(SwingConstants.VERTICAL , 0, 100);”

Fields of JProgressBar

Following are the fields for javax.swing.JProgressBar class :

  • protected ChangeEvent changeEvent: Single ChangeEvent is required per instance because the event's only useful property is the progress bar which is an immutable source.

 

  • protected ChangeListener changeListener: Listens for change events sent by the model of progress, reverting them to change-event listeners registered upon this progress bar.

 

  • protected BoundedRangeModel model: This is the object that keeps the information for the progress bar.

 

  • protected int orientation: This field tells whether the progress bar is vertical or horizontal.

 

  • protected boolean paintBorder: This function of this field is whether to display a border around the progress bar.

 

  • protected boolean paintString:  This function of this field is whether to display a string of text on the progress bar.

 

  • protected String progressString: This is an optional string that can be represented on the progress bar.

Methods of JProgressBar

A method is a set of code that runs only when it is called. We can also pass some data in it, which is known as parameters. Every method performs some specific action. Some of the commonly used methods of JProgressBar are as follows:

  • int getMaximum(): This method of JProgressBar returns the maximum value of the progress bar.

 

  • int getMinimum(): This method of JProgressBar returns the minimum value of the progress bar.

 

  • String getString(): This method of JProgressBar returns the string presentation of the latest value of the progress bar.

 

  • void setMaximum(int a): This method of JProgressBar sets the maximum value of the progress bar to the value of a.

 

  • void setMinimum(int a): This method of JProgressBar sets the minimum value of the progress bar to the value of a.

 

  • void setValue(int a): This method of JProgressBar sets the latest value of the progress bar to the value of a.

 

  • void setString(String a): This method of JProgressBar sets the value of the progress String to String a.

Syntax of JProgressBar

Syntax of JProgressBar is:

"public  class JProgressBar extends Jcomponent implements SwingConstants, Accessible"

Methods Inherited 

JProgressBar also inherits properties  of some classes which are given below:

  • javax.swing.Jcomponent

 

  • java.awt.Container

 

  • java.awt.Component

 

  • java.lang.Component

Example of JProgressBar

In this example we will design a dynamic progress bar.

package jprogressbardemo;
import java.awt.*;
import javax.swing.*;
public class Main {
    public static void main(String[] args) 
    {
         final int MAX = 100;
         final JFrame Jfrm = new JFrame("Gaurav's JProgressBar");
         // creates progress bar
         final JProgressBar JPb = new JProgressBar();
         JPb.setMinimum(0);
         JPb.setMaximum(MAX);
         JPb.setStringPainted(true);
         // add progress bar
         Jfrm.setLayout(new FlowLayout());
         Jfrm.getContentPane().add(JPb);
         Jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         Jfrm.setSize(500, 300);
         Jfrm.setVisible(true);
         // update progressbar
         for (int i = 0; i <= MAX; i++) {
             final int currentValue = i;
             Try
             {
                 SwingUtilities.invokeLater(new Runnable()
                 {
                     public void run()
                     {
                         JPb.setValue(currentValue);
                     }
                 });
                 java.lang.Thread.sleep(100);
             } catch (InterruptedException e)
               {
                 JOptionPane.showMessageDialog(Jfrm, e.getMessage());
             }
         }
     }
 }
You can also try this code with Online Java Compiler
Run Code

 

OUTPUT

This is the progress bar which we have programmed in the above code.

Frequently Asked Questions

How to change the color of JProgressBar?

To change the colour of JProgressBar we use code “UIManager.put("ProgressBar.background", Color.BLUE);”

What is the work of the getMaximum() method in JProgressBar?

The “getMaximum()” returns the maximum value of range of progress bar.

Which constructor is used to create a JProgressBar with defined orientation?

“JProgressBar(int orientation)” creates the orientation of JProgressBar.

Which constructor is used to create the JProgressBar with maximum and minimum values along with its orientation? 

“JProgressBar(int orientation, int min, int max)” is the constructor which creates the JprogressBar with maximum and minimum values along with its orientation.

Which method returns the maximum value of JProgressBar?

“int getMaximum()” is the method that returns the maximum value of JProgressBar.

Conclusion

JProgressBar is one of the essential UI elements that is used frequently to provide feedback to the end-user. This shows the progress of any operation in graphical format. The JProgressBar is generally represented whenever the application gets busy, and also, it is a representation to the user that the system is performing the specified task and it is not stuck.
Also read,

 

Refer to our guided paths on Coding Ninjas Studio to learn more about C programming, DSA, JavaScript, etc. Enrol in our courses and refer to the mock test and problems available. Take a look at the interview experiences and interview bundle for placement preparations.

Do upvote our blog to help other ninjas grow.

Happy Learning!

 

Live masterclass