Previous | Next | Trail Map | Creating a User Interface | Using the JFC/Swing Packages

How to Use Progress Bars and Progress Monitors

[PENDING: currently this page covers only the use of progress bars, but we have plans to expand it to also cover ProgressMonitor(in the API reference documentation) and ProgressMonitorInputStream(in the API reference documentation). ]

Technically speaking, a JProgressBar(in the API reference documentation) displays an integer value within a bounded interval. Realistically speaking, you use a JProgressBar to display the progress of a long-running task. JProgressBar is for display purposes only. To allow the user to set a bounded integer value, use a slider.

Here's a picture of a small demo application that uses a progress bar to measure the progress of a long task running in a separate thread:


Try this:
  1. Compile and run the application. The main source file is ProgressBarDemo.java. You will also need LongTask.java and SwingWorker.java.
    See Getting Started with Swing if you need help.
  2. Push the Start button. Watch the progress bar as the task makes progress.

Below is the code from ProgressBarDemo.java that creates and sets up the progress bar:
progressBar = new JProgressBar();
progressBar.setMinimum(0);
progressBar.setMaximum(task.getLengthOfTask());
progressBar.setValue(0);

. . .
//add the progress bar to the window's content pane
contentPane.add(progressBar);
Once the task has begun, a timer causes the progress bar to update every second until the task completes. The timer is implemented with the Timer class, which is described in How to Use Timers. The progress bar measures the progress made by the task each second, not the elapsed time. The following line of code appears in the actionPerformed method of the timer's action listener.
progressBar.setValue(task.getCurrent());
Once a second, the timer fires an action event, which causes the program to find the amount of work completed by the task and use that value to update the progress bar.

As mentioned, the long-running task in this program runs in a separate thread. The long-running task is implemented by LongTask.java which uses a SwingWorker. See Using the SwingWorker Class in Threads and Swing for information about the SwingWorker class.

The Progress Bar API

The following tables list the commonly used JProgressBar constructors and methods. Other methods you're likely to call are defined by the JComponent(in the API reference documentation) and Component(in the API reference documentation) classes and include [PENDING: anything in particular for JProgressBar?]. [Link to JComponent and Component discussions.]

The API for using progress bars falls into two categories:

Setting or Getting the Progress Bar's Constraints/Values
Method Purpose
void setValue(int)
int getValue()
Set or get the current value of the progress bar. The value is constrained by the minimum and maximum values.
void setMinimum(int)
int getMinimum()
Set or get the minimum value of the progress bar.
void setMaximum(int)
int getMaximum()
Set or get the maximum value of the progress bar.
void setModel(BoundedRangeModel)
BoundedRangeModel getMaximum()
Set or get the model used by the progress bar. The model establishes the progress bar's constraints and values. So you can use this method as an alternative to using the individual set/get methods listed above.

Fine Tuning the Progress Bar's Appearance
Method Purpose
void setOrientation(int)
int getOrientation()
Set or get whether the progress bar is vertical or horizontal. Acceptable values are JProgressBar.VERTICAL or JProgressBar.HORIZONTAL.
void setBorderPainted(boolean)
boolean isBorderPainted()
Set or get whether the progress bar has a border.

Examples that Use JProgressBar

This table shows the examples that use JProgressBar and where those examples are described.

Example Where Described
ProgressBarDemo.java This page and How to Use Timers


Previous | Next | Trail Map | Creating a User Interface | Using the JFC/Swing Packages