JProgressBar Class In Java
JProgressBar Class
In Java, The JProgressBar class displays the progress of the task.
Constructors of JProgressBar class
JProgressBar()
This constructor creates a horizontal progress bar but no string text is there.
JProgressBar(int min, int max)
This constructor creates a horizontal progress bar with the specific minimum and maximum value.
JProgressBar(int orient)
This constructor creates a progress bar with the specific orientation, it can be either Vertical or Horizontal, using SwingConstants. VERTICAL and SwingConstants.HORIZONTAL constants.
JProgressBar(int orient, int min, int max)
This constructor creates a progress bar with the specific orientation, minimum and maximum value.
Methods of JProgressBar class
public void setStringPainted(boolean b)
This method is used to determine whether the string should be displayed or not.
public void setString(String s)
This method is used to set the value to the progress string.
public void setOrientation(int orientation)
This method is used to set the orientation, it may be either vertical or horizontal, using SwingConstants.VERTICAL and SwingConstants.HORIZONTAL constants..
public void setValue(int value)
This method is used to set the current value on the progress bar.
Let’s see an example of JProgressBar class, given below.
Code
- import javax.swing.*;
- public class JProgressBarExample extends JFrame {
- JProgressBar jb;
- int i = 0, num = 0;
- JProgressBarExample() {
- jb = new JProgressBar(0, 3000);
- jb.setBounds(50, 50, 300, 40);
- jb.setValue(0);
- jb.setStringPainted(true);
- add(jb);
- setSize(400, 400);
- setLayout(null);
- }
- public void iterate() {
- while (i <= 3000) {
- jb.setValue(i);
- i = i + 40;
- try {
- Thread.sleep(1500);
- } catch (Exception e) {
- }
- }
- }
- public static void main(String[] args) {
- JProgressBarExample jpb = new JProgressBarExample();
- jpb.setVisible(true);
- jpb.iterate();
- }
- }
Output
Summary
Thus, we learnt that the JProgressBar class displays the progress of the task and also learnt how to use it in Java.