1. Introduction to JProgressBar Component
The JProgressBar Component in swing is for visually representing the progress of a long-running task. It contains a rectangle bar which gets filled when the task approaching towards the end. Have a look at the below picture:

Here you can see a blue Rectangle fills the progress bar. The blue filler here shows the completion of the task and empty bar beside the right signals task to be done. The label in the middle shows the percentage of the completed task.
2. Methods of JProgressBar
In this section, we will study the most frequently used operations of the Java Swing JProgressBar component.
2.1 Minimum and Maximum
The ProgressBar component computes the progress completed based on the maximum, minimum, and current values. We can set minimum value using setMinimum method and the same way maximum value using the setMaximum method.
2.2 Set Color of Progress
In the above screenshot, the bar color is in blue, which is not the default of JProgressBar. One can change the color of the JProgressBar via the method call setForeground which takes an AWT.Color object.
2.3 Access Progress Value
We can retrieve the current progress value through the method call getValue. But the most frequent usage is setValue which will be called periodically by the long-running task to update the task progress. Note, the value will be between minimum and maximum which was set to the JProgressBar. Showing the % complete will aid to the visual appearance as the user can see how much of task completed in terms of the percentage. This can be done through the method call setStringPainted.
3. About the JProgressBar Example
The example which we create here is shown in the below picture:

After the JLabel, we have a JProgressBar component which occupies the entire width of the Frame window. Towards the bottom we have a JButton which will increment the progress bar value on each click. The first checkbox will set the progress maximum to 500 when checked. Second check box will change the JProgressBar Color and the third one will set the progress label on the progress bar. So, to test how ProgressBar works, one can make the combination of the three check boxes and use the JButton to mimic the progress of the long running task.
4. Prepare JPanel & JFrame
The example uses the JFrame derived class JProgressBarExample
. The BorderLayout Manager, manages all the control added to it. In our case (Line 17-19), we create two JPanels
. TopPanel will hold the JProgressBar control and MidPanel will hold the rest of the controls. You can also note that both the panels will make use of GridLayout to arrange the controls.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
@SuppressWarnings("serial") public class JProgressBarExample extends JFrame { //Sample 01: To Store Default ProgressBar Color Color ProgressDefault; @SuppressWarnings({ "rawtypes", "unchecked" }) public JProgressBarExample(String title) throws HeadlessException { super(title); //Sample 02: Set Size and Position setBounds(100, 100, 350, 350); Container ControlHost = getContentPane(); ControlHost.setLayout(new BorderLayout()); //Sample 03: Create Two Panels Panel TopPanel = new Panel(new GridLayout(2,1)); TopPanel.setBackground(Color.BLACK); Panel MidPanel = new Panel(new GridLayout(4,1)); |
How we are going to add these panels to the JFrame is shown in the below picture. Note the grid cells as well here. The top panel is 2×1 and the middle one is 4×1.

5. Load Top Panel with JProgressBar
First, we need to construct the JProgressBar and then we can set the required properties to it. Once the JProgressBar is ready, we can add it to the JPanel which we already created and referenced it with TopPanel. Now have look at the below piece of code:
1 2 3 4 5 6 7 8 9 10 |
//Sample 04: Prepare Top Panel With Progress Bar JLabel lbl = new JLabel("ProgressBar Demo"); lbl.setForeground(Color.WHITE); lbl.setHorizontalAlignment(SwingConstants.CENTER); JProgressBar progress = new JProgressBar(); progress.setValue(0); ProgressDefault = progress.getForeground(); TopPanel.add(lbl); TopPanel.add(progress); ControlHost.add(TopPanel, BorderLayout.NORTH); |
Here, after creating the JLabel, we create JProgressBar at line 5. The call to setValue keeps the progress percentage as 0%. The method getForeground gives the default progress bar color and we store that in member variable for later use. Finally, we add both the components to the JPanel called TopPanel and fit that in the north part of the JFrame.
6. Prepare Middle Panel
The middle panel contains three check boxes and a button. In the below code, we create the check boxes via JCheckBox class and a button via JButton. Then, we add all these 4 components to the MidPanel. This middle panel sits in the centre location of the JFrame’s Border Layout.
1 2 3 4 5 6 7 8 9 10 11 |
//Sample 05: Prepare Middle Panel JCheckBox chkMax = new JCheckBox( "Maximum 500 when Checked or Maximum is 100"); JCheckBox chkColor = new JCheckBox("Blue When Cheked"); JCheckBox chkPaint = new JCheckBox("Paint Progress String"); JButton btn = new JButton("Mimic Long Running Task"); MidPanel.add(chkMax); MidPanel.add(chkColor); MidPanel.add(chkPaint); MidPanel.add(btn); ControlHost.add(MidPanel); |
7. Read & Write Progress
The getValue and setValue methods are used to read or write progress value on the JProgressBar. Have a look at the button click handler code below:
1 2 3 4 5 6 7 8 |
//Sample 06: Add Button Click Handler btn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { int x = progress.getValue(); progress.setValue(x + 10); } }); |
At line 5, we read the current value in the JProgressBar and increment it by 10. Then we make a call to the setValue with this incremented progress value. So, this mimics the Progress Bar update. On each click, the progress value increments by 10.
8. JProgressBar Minimum & Maximum
The method
setMaximum sets the maximum value to the JProgressBar component. Handler method itemStateChanged
checks the checkbox state and sets the maximum value as 500 when it finds the user placed a check mark on the First Checkbox in the UI. When the maximum value of the Progress Bar is 500, we need to click the button more number of times to reach the 100% state. Note, you can also use
setMinimum function to denote the start of the progress and apply change from there. In our case, the start point is 0.
1 2 3 4 5 6 7 8 9 10 |
//Sample 07: Set Maximum to 500 or 100 chkMax.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { progress.setValue(10); if (chkMax.isSelected()) progress.setMaximum(500); else progress.setMaximum(100); } }); |
9. Progress Color
When the user clicks the second check box, we should change the Progress Bar color to blue. In the below code, we handle the ItemEvent in the handler function itemStateChanged
. The handler function tests the checked state of the second check box and sets the Java Swing JProgressBar color using the
setForeground method. When the checkbox in checked state, we set the blue color, otherwise we default to original JProgressBar colour.
1 2 3 4 5 6 7 8 9 10 11 12 |
//Sample 08: Set New Foreground Color chkColor.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { if (chkColor.isSelected()) { progress.setForeground(Color.BLUE); } else progress.setForeground(ProgressDefault); } }); |
10. Progress Status Label
Like we did for the previous two checkboxes, we handle the ItemEvent
of the third checkbox as well. The method
setStringPainted will accept a boolean param and when it is true, Java Swing will paint the Progress Percentage in the JProgressBar component. Note, the percentage here is signaling how much progress completed. It will be computed by JProgressBar Swing Class by reading the values Minimum, Maximum, and current value of progress.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
//Sample 09: Set New Foreground Color chkPaint.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { if (chkPaint.isSelected()) { progress.setStringPainted(true); } else progress.setStringPainted(false); } }); |
11. Youtube: Code Implementation
12. Code Reference
12.1 MainEntry.java
1 2 3 4 5 6 7 8 9 |
package tube.coding.examples; public class MainEntry { public static void main(String[] args) { //Sample 06: Create Instance of JFrameDemo JProgressBarExample frame = new JProgressBarExample("JProgressBar Example"); frame.setVisible(true); } } |
12.2 JProgressBarExample.java
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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
package tube.coding.examples; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.GridLayout; import java.awt.HeadlessException; import java.awt.Panel; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JProgressBar; import javax.swing.SwingConstants; @SuppressWarnings("serial") public class JProgressBarExample extends JFrame { //Sample 01: To Store Default ProgressBar Color Color ProgressDefault; @SuppressWarnings({ "rawtypes", "unchecked" }) public JProgressBarExample(String title) throws HeadlessException { super(title); //Sample 02: Set Size and Position setBounds(100, 100, 350, 350); Container ControlHost = getContentPane(); ControlHost.setLayout(new BorderLayout()); //Sample 03: Create Two Panels Panel TopPanel = new Panel(new GridLayout(2,1)); TopPanel.setBackground(Color.BLACK); Panel MidPanel = new Panel(new GridLayout(4,1)); //Sample 04: Prepare Top Panel With Progress Bar JLabel lbl = new JLabel("ProgressBar Demo"); lbl.setForeground(Color.WHITE); lbl.setHorizontalAlignment(SwingConstants.CENTER); JProgressBar progress = new JProgressBar(); progress.setValue(0); ProgressDefault = progress.getForeground(); TopPanel.add(lbl); TopPanel.add(progress); ControlHost.add(TopPanel, BorderLayout.NORTH); //Sample 05: Prepare Middle Panel JCheckBox chkMax = new JCheckBox("Maximum 500 when Checked or Maximum is 100"); JCheckBox chkColor = new JCheckBox("Blue When Cheked"); JCheckBox chkPaint = new JCheckBox("Paint Progress String"); JButton btn = new JButton("Mimic Long Running Task"); MidPanel.add(chkMax); MidPanel.add(chkColor); MidPanel.add(chkPaint); MidPanel.add(btn); ControlHost.add(MidPanel); //Sample 06: Add Button Click Handler btn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { int x = progress.getValue(); progress.setValue(x + 10); } }); //Sample 07: Set Maximum to 500 or 100 chkMax.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { progress.setValue(10); if (chkMax.isSelected()) progress.setMaximum(500); else progress.setMaximum(100); } }); //Sample 08: Set New Foreground Color chkColor.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { if (chkColor.isSelected()) { progress.setForeground(Color.BLUE); } else progress.setForeground(ProgressDefault); } }); //Sample 09: Set New Foreground Color chkPaint.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { if (chkPaint.isSelected()) { progress.setStringPainted(true); } else progress.setStringPainted(false); } }); } } |
Categories: Swing
Tags: getForeground, getValue, setForeGround, setMaximum, setMinimum, setStringPainted, setValue