ProgressBar In Java Example
Chapter:
Swing
Last Updated:
22-09-2018 08:08:14 UTC
Program:
/* ............... START ............... */
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
public class JavaSwingProgressBarExample extends JFrame {
JProgressBar current = new JProgressBar(0, 2000);
int num = 0;
public JavaSwingProgressBarExample() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel pane = new JPanel();
current.setValue(0);
current.setStringPainted(true);
pane.add(current);
setContentPane(pane);
}
public void iterate() {
while (num < 2000) {
current.setValue(num);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
num += 95;
}
}
public static void main(String[] arguments) {
JavaSwingProgressBarExample frame = new JavaSwingProgressBarExample();
frame.pack();
frame.setVisible(true);
frame.iterate();
}
}
/* ............... END ............... */
Output
Notes:
-
The class JProgressBar is a component which visually displays the progress of some task.
- JProgressBar(): is used to create a horizontal progress bar but no string text.
- JProgressBar(int min, int max): is used to create a horizontal progress bar with the specified minimum and maximum value.
- JProgressBar(int orient): is used to create a progress bar with the specified orientation, it can be either Vertical or Horizontal.
- JProgressBar(int orient, int min, int max): is used to create a progress bar with the specified orientation, minimum and maximum value.
Tags
ProgressBar, Java, Swing, java progress bar show percentage, java progress bar thread example,java swing progress bar dialog