Tuesday, November 19, 2024
Google search engine
HomeLanguagesJavaJava Swing | JProgressBar

Java Swing | JProgressBar

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 : 
 

  1. JProgressBar() : creates an progress bar with no text on it;
  2. 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.
  3. JProgressBar(int min, int max) : creates an progress bar with specified minimum and maximum value.
  4. 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 : 
 

  1. int getMaximum() : returns the progress bar’s maximum value.
  2. int getMinimum() : returns the progress bar’s minimum value.
  3. String getString() : get the progress bar’s string representation of current value.
  4. void setMaximum(int n) : sets the progress bar’s maximum value to the value n.
  5. void setMinimum(int n) : sets the progress bar’s minimum value to the value n.
  6. void setValue(int n) : set Progress bar’s current value to the value n.
  7. 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 bar
import 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 bar
import 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 progressbar
import 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
 

RELATED ARTICLES

Most Popular

Recent Comments