public class JProgressBar extends JComponent implements SwingConstants, Accessible
JProgressBar
uses a BoundedRangeModel
as its data model,
with the value
property representing the "current" state of the task,
and the minimum
and maximum
properties representing the
beginning and end points, respectively.
To indicate that a task of unknown length is executing, you can put a progress bar into indeterminate mode. While the bar is in indeterminate mode, it animates constantly to show that work is occurring. As soon as you can determine the task's length and amount of progress, you should update the progress bar's value and switch it back to determinate mode.
Here is an example of creating a progress bar,
where task
is an object (representing some piece of work)
which returns information about the progress of the task:
progressBar = new JProgressBar(0, task.getLengthOfTask()); progressBar.setValue(0); progressBar.setStringPainted(true);Here is an example of querying the current state of the task, and using the returned value to update the progress bar:
progressBar.setValue(task.getCurrent());Here is an example of putting a progress bar into indeterminate mode, and then switching back to determinate mode once the length of the task is known:
progressBar = new JProgressBar(); ...//when the task of (initially) unknown length begins: progressBar.setIndeterminate(true); ...//do some work; get length of task... progressBar.setMaximum(newLength); progressBar.setValue(newValue); progressBar.setIndeterminate(false);
For complete examples and further documentation see How to Monitor Progress, a section in The Java Tutorial.
Warning: Swing is not thread safe. For more information see Swing's Threading Policy.
Warning:
Serialized objects of this class will not be compatible with
future Swing releases. The current serialization support is
appropriate for short term storage or RMI between applications running
the same version of Swing. As of 1.4, support for long term storage
of all JavaBeans™
has been added to the java.beans
package.
Please see XMLEncoder
.
BasicProgressBarUI
,
BoundedRangeModel
,
SwingWorker
Modifier and Type | Class | Description |
---|---|---|
protected class |
JProgressBar.AccessibleJProgressBar |
This class implements accessibility support for the
JProgressBar class. |
JComponent.AccessibleJComponent
Container.AccessibleAWTContainer
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
Modifier and Type | Field | Description |
---|---|---|
protected ChangeEvent |
changeEvent |
Only one
ChangeEvent is needed per instance since the
event's only interesting property is the immutable source, which
is the progress bar. |
protected ChangeListener |
changeListener |
Listens for change events sent by the progress bar's model,
redispatching them
to change-event listeners registered upon
this progress bar.
|
protected BoundedRangeModel |
model |
The object that holds the data for the progress bar.
|
protected int |
orientation |
Whether the progress bar is horizontal or vertical.
|
protected boolean |
paintBorder |
Whether to display a border around the progress bar.
|
protected boolean |
paintString |
Whether to display a string of text on the progress bar.
|
protected String |
progressString |
An optional string that can be displayed on the progress bar.
|
listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
accessibleContext, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
BOTTOM, CENTER, EAST, HORIZONTAL, LEADING, LEFT, NEXT, NORTH, NORTH_EAST, NORTH_WEST, PREVIOUS, RIGHT, SOUTH, SOUTH_EAST, SOUTH_WEST, TOP, TRAILING, VERTICAL, WEST
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
Constructor | Description |
---|---|
JProgressBar() |
Creates a horizontal progress bar
that displays a border but no progress string.
|
JProgressBar(BoundedRangeModel newModel) |
Creates a horizontal progress bar
that uses the specified model
to hold the progress bar's data.
|
JProgressBar(int orient) |
Creates a progress bar with the specified orientation,
which can be
either
SwingConstants.VERTICAL or
SwingConstants.HORIZONTAL . |
JProgressBar(int min,
int max) |
Creates a horizontal progress bar
with the specified minimum and maximum.
|
JProgressBar(int orient,
int min,
int max) |
Creates a progress bar using the specified orientation,
minimum, and maximum.
|
Modifier and Type | Method | Description |
---|---|---|
void |
addChangeListener(ChangeListener l) |
Adds the specified
ChangeListener to the progress bar. |
protected ChangeListener |
createChangeListener() |
Subclasses that want to handle change events
from the model differently
can override this to return
an instance of a custom
ChangeListener implementation. |
protected void |
fireStateChanged() |
Send a
ChangeEvent , whose source is this JProgressBar , to
all ChangeListener s that have registered interest in
ChangeEvent s. |
AccessibleContext |
getAccessibleContext() |
Gets the
AccessibleContext associated with this
JProgressBar . |
ChangeListener[] |
|