is new.
java.lang.Objectjava.awt.Component
java.awt.Container
javax.swing.JComponent
javax.swing.JProgressBar
public class JProgressBar
A component that visually displays the progress of some task. As the task progresses towards completion, the progress bar displays the task's percentage of completion. This percentage is typically represented visually by a rectangle which starts out empty and gradually becomes filled in as the task progresses. In addition, the progress bar can display a textual representation of this percentage.
A component that, by default, displays an integer value within a bounded interval. A progress bar typically communicates the progress of some work by displaying its percentage of completion and possibly a textual display of this percentage.
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
that
returns information about the progress of
the task:
some work:
progressBar = new JProgressBar(0, task.getLengthOfTask()); progressBar.setValue(0); progressBar.setStringPainted(true);Here is an example of
querying
current state
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 TM has been added to the java.beans package. Please see XMLEncoder .
,
BoundedRangeModel
,
SwingWorker
| Nested Class Summary | |
|---|---|
| protected class |
JProgressBar.AccessibleJProgressBar
This class implements accessibility support for the JProgressBar class. |
| Nested classes/interfaces inherited from class javax.swing. JComponent |
|---|
| JComponent.AccessibleJComponent |
| Nested classes/interfaces inherited from class java.awt. Container |
|---|
| Container.AccessibleAWTContainer |
| Nested classes/interfaces inherited from class java.awt. Component |
|---|
| Component.AccessibleAWTComponent , Component.BaselineResizeBehavior , Component.BltBufferStrategy , Component.FlipBufferStrategy |
| Field Summary | |
|---|---|
| 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
of text
on the progress bar. |
| protected String |
progressString
An optional string that can be displayed on the progress bar. |
| Fields inherited from class javax.swing. JComponent |
|---|
| accessibleContext , listenerList , TOOL_TIP_TEXT_KEY , ui , UNDEFINED_CONDITION , WHEN_ANCESTOR_OF_FOCUSED_COMPONENT , WHEN_FOCUSED , WHEN_IN_FOCUSED_WINDOW |
| Fields inherited from class java.awt. Component |
|---|
| BOTTOM_ALIGNMENT , CENTER_ALIGNMENT , LEFT_ALIGNMENT , RIGHT_ALIGNMENT , TOP_ALIGNMENT |
| Fields inherited from interface javax.swing. SwingConstants |
|---|
| BOTTOM , CENTER , EAST , HORIZONTAL , LEADING , LEFT , NEXT , NORTH , NORTH_EAST , NORTH_WEST , PREVIOUS , RIGHT , SOUTH , SOUTH_EAST , SOUTH_WEST , TOP , TRAILING , VERTICAL , WEST |
| Fields inherited from interface java.awt.image. ImageObserver |
|---|
| ABORT , ALLBITS , ERROR , FRAMEBITS , HEIGHT , PROPERTIES , SOMEBITS , WIDTH |
| Constructor Summary | |
|---|---|
|
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
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. |
|
| Method Summary | |
|---|---|
| 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
ChangeListeners
|
| AccessibleContext |
getAccessibleContext
() Gets the AccessibleContext associated with this JProgressBar. |
| ChangeListener [] |
getChangeListeners
() Returns an array of all the ChangeListeners added to this progress bar with addChangeListener. |
| int |
getMaximum
() Returns the progress bar's maximum
value from
|
| int |
getMinimum
() Returns the progress bar's minimum
value from
|
| BoundedRangeModel |
getModel
() Returns the data model used by this progress bar. |
| int |
getOrientation
() Returns
SwingConstants.VERTICAL
SwingConstants.HORIZONTAL,
|
| double |
getPercentComplete
() Returns the percent complete for the progress bar. |
| String |
getString
() Returns
a String representation
current progress.
|
| ProgressBarUI |
getUI
() Returns the look-and-feel object that renders this component. |
| String |
getUIClassID
() Returns the name of the look-and-feel class that renders this component. |
| int |
getValue
() Returns the progress bar's current
value from
|
| boolean |
isBorderPainted
() Returns the borderPainted property. |
| boolean |
isIndeterminate
() Returns the value of the indeterminate property. |
| boolean |
isStringPainted
() Returns the value of the stringPainted property. |
| protected void |
paintBorder
(
Graphics
g) Paints the progress bar's border if the borderPainted property is true. |
| protected String |
paramString
() Returns a string representation of this JProgressBar. |
| void |
removeChangeListener
(
ChangeListener
l) Removes a ChangeListener from the progress bar. |
| void |
setBorderPainted
(boolean b) Sets the borderPainted property, which is true if the progress bar should paint its border. |
| void |
setIndeterminate
(boolean newValue) Sets the indeterminate property of the progress bar, which determines whether the progress bar is in determinate or indeterminate mode. |
| void |
setMaximum
(int n) Sets the progress bar's maximum value (stored in the progress bar's data model) to n. |
| void |
setMinimum
(int n) Sets the progress bar's minimum value (stored in the progress bar's data model) to n. |
| void |
setModel
(
BoundedRangeModel
newModel) Sets the data model used by the JProgressBar. |
| void |
setOrientation
(int newOrientation) Sets the progress bar's orientation to newOrientation, which must be
SwingConstants.VERTICAL
SwingConstants.HORIZONTAL.
|
| void |
setString
(
String
s) Sets the value of the progress string. |
| void |
setStringPainted
(boolean b) Sets the value of the stringPainted property, which determines whether the progress bar should render a progress string. |
| void |
setUI
(
ProgressBarUI
ui) Sets the look-and-feel object that renders this component. |
| void |
setValue
(int n) Sets the progress bar's current value
|
| void |
updateUI
() Resets the UI property to a value from the current look and feel. |
| Methods inherited from class java.lang. Object |
|---|
| clone , equals , finalize , getClass , hashCode , notify , notifyAll , wait , wait , wait |
| Field Detail |
|---|
protected int orientation
protected boolean paintBorder
protected BoundedRangeModel model
protected String progressString
To display the string, paintString must be true.
,
setStringPainted(boolean)
protected boolean paintString
of text
on the progress bar. The default is false. Setting this to true causes a textual display of the progress to be rendered on the progress bar. If the progressString is null, the percentage of completion is displayed on the progress bar. Otherwise, the progressString is rendered on the progress bar.
,
setString(java.lang.String)
protected transient ChangeEvent changeEvent
The event is lazily created the first time that an event notification is fired.
See Also:
fireStateChanged()
protected ChangeListener changeListener
| Constructor Detail |
|---|
public JProgressBar()
public JProgressBar(int orient)
SwingConstants.VERTICAL
SwingConstants.HORIZONTAL.
Throws:
IllegalArgumentException
- if orient is an illegal value
public JProgressBar(int min,
int max)
The BoundedRangeModel that holds the progress bar's data handles any issues that may arise from improperly setting the minimum, initial, and maximum values on the progress bar.
See the BoundedRangeModel documentation for details.
public JProgressBar(int orient,
int min,
int max)
The BoundedRangeModel that holds the progress bar's data handles any issues that may arise from improperly setting the minimum, initial, and maximum values on the progress bar.
See the BoundedRangeModel documentation for details.
Throws:
IllegalArgumentException
- if orient is an illegal value
public JProgressBar(BoundedRangeModel newModel)
| Method Detail |
|---|
public int getOrientation()
SwingConstants.VERTICAL
SwingConstants.HORIZONTAL,
SwingConstants.HORIZONTAL.
public void setOrientation(int newOrientation)
SwingConstants.VERTICAL
SwingConstants.HORIZONTAL.
SwingConstants.HORIZONTAL.
public boolean isStringPainted()
public void setStringPainted(boolean b)
false, meaning
public String getString()
Returns a String representation of the current progress. By default, this returns a simple percentage String based on the value returned from getPercentComplete. An example would be the "42%". You can change this by calling setString.
the value of the progress string, or a simple percentage string if the progress string is null
public void setString(String s)
By default, this string is null, implying the built-in behavior of using a simple percent string.
If you are providing a custom progress string by overriding this method, make sure that you call setString before calling getString.
The progress string is painted only if the isStringPainted method returns true.
progress
public double getPercentComplete()
public boolean isBorderPainted()
public void setBorderPainted(boolean b)
protected void paintBorder(Graphics g)
public ProgressBarUI getUI()
public void setUI(ProgressBarUI ui)
public void updateUI()
public String getUIClassID()
protected ChangeListener createChangeListener()
The default ChangeListener simply calls the fireStateChanged method to forward ChangeEvents to the ChangeListeners that have been added directly to the progress bar.
,
fireStateChanged()
,
ChangeListener
,
BoundedRangeModel
public void addChangeListener(ChangeListener l)
public void removeChangeListener(ChangeListener l)
public ChangeListener[] getChangeListeners()
protected void fireStateChanged()
Send a ChangeEvent, whose source is this JProgressBar, to all ChangeListeners that have registered interest in ChangeEvents. This method is called each time a ChangeEvent is received from the model.
The event instance is created if necessary, and stored in changeEvent.
addChangeListener(javax.swing.event.ChangeListener)
,
EventListenerList
public BoundedRangeModel getModel()
setModel(javax.swing.BoundedRangeModel)
,
BoundedRangeModel
public void setModel(BoundedRangeModel newModel)
Note that the BoundedRangeModel's extent is not used, and is set to 0.
public int getValue()
value from
public int getMinimum()
Returns the progress bar's minimum value from the BoundedRangeModel.
public int getMaximum()
Returns the progress bar's maximum value from the BoundedRangeModel.
public void setValue(int n)
Sets the progress bar's current value to n. This method forwards the new value to the model.
The data model (an instance of BoundedRangeModel) handles any mathematical issues arising from assigning faulty values. See the BoundedRangeModel documentation for details.
If the new value is different from the previous value, all change listeners are notified.
,
addChangeListener(javax.swing.event.ChangeListener)
,
BoundedRangeModel.setValue(int)
public void setMinimum(int n)
The data model (a BoundedRangeModel instance) handles any mathematical issues arising from assigning faulty values.
See the BoundedRangeModel documentation for details.
If the minimum value is different from the previous minimum, all change listeners are notified.
public void setMaximum(int n)
The underlying BoundedRangeModel handles any mathematical issues arising from assigning faulty values.
See the BoundedRangeModel documentation for details.
If the maximum value is different from the previous maximum, all change listeners are notified.
public void setIndeterminate(boolean newValue)
See How to Monitor Progress for examples of using indeterminate progress bars.
public boolean isIndeterminate()
protected String paramString()
public AccessibleContext getAccessibleContext()