In this tutorial you will learn about the Swing JProgessBar and its application with practical example.
Swing JProgessBar
JProgressBar is used to displays the progress of some task. As the task progresses towards completion , the progress bar displays task’s percentage of completion.
JProgessBarClass Declaration
1 |
Public class JProgressBar extends JComponent implements SwingConstant, Accessible |
JProgessBar Constructor
Let’s get more understand about JProgressBaar, Some of important constructor are listed below.
JProgressBar(): This constructor is used to construct a horizontal progress bar without progress string.
JProgessBar(BoundedRangModel newModel): This constructor is used to construct a horizontal progress bar that hold the progress bar’s data of specified model.
JProgressBar(int orient): This constructor construct a progress bar with given orientation, it may be SwingConstant.VERTICAL or SwingConstant.Horizontal.
JProgressBar(int min, int max): This constructor construct a horizontal progress bar with the minimum and maximum given value.
JProgressBar(int orient, int min, int max): This constructor construct a progress bar with minimum , maximum and given orientation.
JProgessBar Method
Some of commonly used methods are listed below. It will help to know more about JProgressBar
void addChangeListener(ChangeListener l): This method is used to add the specified ChangeListener to the progress bar.
int getValue(): It returns current progress value of Progress Bar.
boolean isIndeterminate(): This method returns the value of the indeterminate property.
void setIndeterminate(boolean value): This method is used to set Indeterminate value of Progress Bar , which specified that the progress bar will be determinate or indeterminate.
void setMaximum(int n): It sets the maximum progress value of progress bar.
void setMinimum(int n): It sets the minimum progress value of progress bar.
void setString(String s): it is used to set the value of the progress string.
void setValue(int n):This method is used to sets the “n”current progress value to the Progress Bar.
Example :-
An simple example to get understand about JProgressBar. Hopefully this will work for you.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
package w3addaswing; import javax.swing.JFrame; import javax.swing.JProgressBar; public class W3ADDASwing { JProgressBar jprogressBar; int progress = 0, num = 0; W3ADDASwing() { JFrame frame = new JFrame("JProgressBar Example W3Adda"); jprogressBar = new JProgressBar(0, 2000); jprogressBar.setBounds(40, 40, 160, 30); jprogressBar.setValue(0); jprogressBar.setStringPainted(true); frame.add(jprogressBar); frame.setSize(400, 400); frame.setLayout(null); frame.setVisible(true); } public void iterate() { while (progress <= 2000) { jprogressBar.setValue(progress); progress = progress + 10; try { Thread.sleep(250); } catch (Exception e) { } } } public static void main(String[] args) { // TODO code application logic here W3ADDASwing w3s=new W3ADDASwing(); w3s.iterate(); } } |
Output :-
Output of this code will look like below image. You just need to copy this code and paste it in you program.