Java Swing | JProgressBar

14 Apr, 2021
JProgressBar is a part of Java Swing package. JProgressBar visually displays the progress of some specified task. JProgressBar shows the percentage of completion of specified task.The progress bar fills up as the task reaches it completion. In addition to show the percentage of completion of task, it can also display some text .Constructors of JProgressBar :  
JProgressBar() : creates an progress bar with no text on it;JProgressBar(int orientation) : creates an progress bar with a specified orientation. if SwingConstants.VERTICAL is passed as argument a vertical progress bar is created, if SwingConstants.HORIZONTAL is passed as argument a horizontal progress bar is created.JProgressBar(int min, int max) : creates an progress bar with specified minimum and maximum value.JProgressBar(int orientation, int min, int max) : creates an progress bar with specified minimum and maximum value and a specified orientation.if SwingConstants.VERTICAL is passed as argument a vertical progress bar is created, if SwingConstants.HORIZONTAL is passed as argument a horizontal progress bar is created.  
JProgressBar() : creates an progress bar with no text on it;
JProgressBar(int orientation) : creates an progress bar with a specified orientation. if SwingConstants.VERTICAL is passed as argument a vertical progress bar is created, if SwingConstants.HORIZONTAL is passed as argument a horizontal progress bar is created.
JProgressBar(int min, int max) : creates an progress bar with specified minimum and maximum value.
JProgressBar(int orientation, int min, int max) : creates an progress bar with specified minimum and maximum value and a specified orientation.if SwingConstants.VERTICAL is passed as argument a vertical progress bar is created, if SwingConstants.HORIZONTAL is passed as argument a horizontal progress bar is created.  
Commonly used methods of JProgressBar are :  
int getMaximum() : returns the progress bar’s maximum value.int getMinimum() : returns the progress bar’s minimum value.String getString() : get the progress bar’s string representation of current value.void setMaximum(int n) : sets the progress bar’s maximum value to the value n.void setMinimum(int n) : sets the progress bar’s minimum value to the value n.void setValue(int n) : set Progress bar’s current value to the value n.void setString(String s) : set the value of the progress String to the String s.
int getMaximum() : returns the progress bar’s maximum value.
int getMinimum() : returns the progress bar’s minimum value.
String getString() : get the progress bar’s string representation of current value.
void setMaximum(int n) : sets the progress bar’s maximum value to the value n.
void setMinimum(int n) : sets the progress bar’s minimum value to the value n.
void setValue(int n) : set Progress bar’s current value to the value n.
void setString(String s) : set the value of the progress String to the String s.
1. Program to create a simple progress bar  
Java
// Java Program to create a// simple progress barimport java.awt.*;import javax.swing.*;import java.awt.event.*;public class progress extends JFrame {     // create a frame    static JFrame f;     static JProgressBar b;     public static void main()    {         // create a frame        f = new JFrame("ProgressBar demo");         // create a panel        JPanel p = new JPanel();         // create a progressbar        b = new JProgressBar();         // set initial value        b.setValue(0);         b.setStringPainted(true);         // add progressbar        p.add(b);         // add panel        f.add(p);         // set the size of the frame        f.setSize(500, 500);        f.setVisible(true);         fill();    }     // function to increase progress    public static void fill()    {        int i = 0;        try {            while (i <= 100) {                // fill the menu bar                b.setValue(i + 10);                 // delay the thread                Thread.sleep(1000);                i += 20;            }        }        catch (Exception e) {        }    }}
OUTPUT :  
2. Program to create a vertical progress bar  
Java
// Java program to create a// vertical progress barimport java.awt.*;import javax.swing.*;import java.awt.event.*;public class progress extends JFrame {     // create a frame    static JFrame f;     static JProgressBar b;     public static void main()    {         // create a frame        f = new JFrame("ProgressBar demo");         // create a panel        JPanel p = new JPanel();         // create a progressbar        b = new JProgressBar(SwingConstants.VERTICAL);         // set initial value        b.setValue(0);         b.setStringPainted(true);         // add progressbar        p.add(b);         // add panel        f.add(p);         // set the size of the frame        f.setSize(500, 500);        f.setVisible(true);         fill();    }     // function to increase progress    public static void fill()    {        int i = 0;        try {            while (i <= 100) {                // fill the menu bar                b.setValue(i + 10);                 // delay the thread                Thread.sleep(1000);                i += 20;            }        }        catch (Exception e) {        }    }}
OUTPUT :  
3. Program to set specific string to progressbar  
Java
// Java Program to set specific// string to progressbarimport java.awt.*;import javax.swing.*;import java.awt.event.*;public class progress extends JFrame {     // create a frame    static JFrame f;     static JProgressBar b;     public static void main()    {         // create a frame        f = new JFrame("ProgressBar demo");         // create a panel        JPanel p = new JPanel();         // create a progressbar        b = new JProgressBar();         // set initial value        b.setValue(0);         b.setStringPainted(true);         // add progressbar        p.add(b);         // add panel        f.add(p);         // set the size of the frame        f.setSize(500, 500);        f.setVisible(true);         fill();    }     // function to increase progress    public static void fill()    {        int i = 0;        try {            while (i <= 100) {                // set text according to the level to which the bar is filled                if (i > 30 && i < 70)                    b.setString("wait for sometime");                else if (i > 70)                    b.setString("almost finished loading");                else                    b.setString("loading started");                 // fill the menu bar                b.setValue(i + 10);                 // delay the thread                Thread.sleep(3000);                i += 20;            }        }        catch (Exception e) {        }    }}
OUTPUT :  
 
 
Note : the above program will not run in an online compiler please use an offline IDE 
sweetyty
java-swing
Java
Programming Language
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Object Oriented Programming (OOPs) Concept in Java
How to iterate any Map in Java
Interfaces in Java
HashMap in Java with Examples
Stream In Java
Differences between Procedural and Object Oriented Programming
Arrow operator -> in C/C++ with Examples
Modulo Operator (%) in C/C++ with Examples
Structures in C++
Decorators with parameters in Python