Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)

JProgressBar – Long Running Task Progress

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:

JProgressBar - Parts
JProgressBar – Parts

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.

Video 1: Basics of JProgressBar

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.

Video 2: About the Example

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.

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.

Panel Arrangement
Panel Arrangement

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:

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.

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:

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.

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.

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.

11. Youtube: Code Implementation

12. Code Reference



Categories: Swing

Tags: , , , , , ,

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.