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 java.
.
javax.swing.plaf.basic.BasicProgressBarUI
, javax.swing.BoundedRangeModel
, javax.swing.SwingWorker
Modifier and Type | Class and Description |
---|---|
protected class | JProgressBar.
This class implements accessibility support for the
|
private class |
Modifier and Type | Field and Description |
---|---|
protected transient ChangeEvent | changeEvent
Only one |
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. |
private static final int | defaultMaximum
The default maximum for a progress bar is 100. |
private static final int | defaultMinimum
The default minimum for a progress bar is 0. |
private static final int | defaultOrientation
The default orientation for a progress bar is |
private transient Format | format
Format used when displaying percent complete. |
private boolean | indeterminate
Whether the progress bar is indeterminate ( |
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. |
private static final String |
Access | Constructor and Description |
---|---|
public | |
public | JProgressBar(int
the desired orientation of the progress bar orient)Creates a progress bar with the specified orientation,
which can be
either |
public | JProgressBar(int
the minimum value of the progress bar min, int the maximum value of the progress bar max)Creates a horizontal progress bar with the specified minimum and maximum. |
public | JProgressBar(int
the desired orientation of the progress bar orient, int the minimum value of the progress bar min, int the maximum value of the progress bar max)Creates a progress bar using the specified orientation, minimum, and maximum. |
public | JProgressBar(BoundedRangeModel
the data model for the progress bar newModel)Creates a horizontal progress bar that uses the specified model to hold the progress bar's data. |
Modifier and Type | Method and Description |
---|---|
public void | addChangeListener(ChangeListener
the l)ChangeListener to addAdds the specified |
protected ChangeListener | Returns: the instance of a customChangeListener implementation.Subclasses that want to handle change events
from the model differently
can override this to return
an instance of a custom |
protected void | fireStateChanged()
Send a |
public AccessibleContext | Returns: anAccessibleJProgressBar that serves as the
AccessibleContext of this JProgressBar Overrides java. Implements javax. AccessibleContext associated with this
JProgressBar .
|
public ChangeListener[] | Returns: all of theChangeListener s added or an empty
array if no listeners have been addedReturns an array of all the |
public int | Returns: the progress bar's maximum valueReturns the progress bar's |
public int | Returns: the progress bar's minimum valueReturns the progress bar's |
public BoundedRangeModel | Returns: theBoundedRangeModel currently in useReturns the data model used by this progress bar. |
public int | Returns: HORIZONTAL or VERTICAL Returns |
public double | Returns: the percent complete for this progress barReturns the percent complete for the progress bar. |
public String | Returns: the value of the progress string, or a simple percentage string if the progress string isnull Returns a |
public ProgressBarUI | Returns: theProgressBarUI object that renders this componentOverrides javax. |
public String | Returns: the string "ProgressBarUI"Overrides javax. |
public int | Returns: the current value of the progress barReturns the progress bar's current |
public boolean | Returns: the value of theborderPainted propertyReturns the |
public boolean | Returns: the value of theindeterminate propertyReturns the value of the |
public boolean | Returns: the value of thestringPainted propertyReturns the value of the |
protected void | paintBorder(Graphics
the g)Graphics context within which to paint the borderOverrides javax. borderPainted
property is true .
|
protected String | Returns: a string representation of thisJProgressBar Overrides javax. JProgressBar .
|
public void | removeChangeListener(ChangeListener
the l)ChangeListener to removeRemoves a |
public void | setBorderPainted(boolean
true if the progress bar
should paint its border;
otherwise, false Sets the |
public void | setIndeterminate(boolean
true if the progress bar
should change to indeterminate mode;
false if it should revert to normal.Sets the |
public void | setMaximum(int
the new maximum n)Sets the progress bar's maximum value
(stored in the progress bar's data model) to |
public void | setMinimum(int
the new minimum n)Sets the progress bar's minimum value
(stored in the progress bar's data model) to |
public void | setModel(BoundedRangeModel
the newModel)BoundedRangeModel to useSets the data model used by the |
public void | setOrientation(int
HORIZONTAL or VERTICAL Sets the progress bar's orientation to |
public void | |
public void | setStringPainted(boolean
true if the progress bar should render a stringSets the value of the |
public void | setUI(ProgressBarUI
a ui)ProgressBarUI objectSets the look-and-feel object that renders this component. |
public void | |
public void | updateUI()
Overrides javax. |
private void | writeObject(ObjectOutputStream
the s)
See readObject() and writeObject() in JComponent for more
information about serialization in Swing.
ObjectOutputStream in which to write |
changeEvent | back to summary |
---|---|
protected transient ChangeEvent changeEvent Only one
|
changeListener | back to summary |
---|---|
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.
|
defaultMaximum | back to summary |
---|---|
private static final int defaultMaximum The default maximum for a progress bar is 100. |
defaultMinimum | back to summary |
---|---|
private static final int defaultMinimum The default minimum for a progress bar is 0. |
defaultOrientation | back to summary |
---|---|
private static final int defaultOrientation The default orientation for a progress bar is |
format | back to summary |
---|---|
private transient Format format Format used when displaying percent complete. |
indeterminate | back to summary |
---|---|
private boolean indeterminate Whether the progress bar is indeterminate (
|
model | back to summary |
---|---|
protected BoundedRangeModel model The object that holds the data for the progress bar.
|
orientation | back to summary |
---|---|
protected int orientation Whether the progress bar is horizontal or vertical.
The default is
|
paintBorder | back to summary |
---|---|
protected boolean paintBorder Whether to display a border around the progress bar.
The default is
|
paintString | back to summary |
---|---|
protected boolean paintString Whether to display a string of text on the progress bar.
The default is
|
progressString | back to summary |
---|---|
protected String progressString An optional string that can be displayed on the progress bar.
The default is
|
uiClassID | back to summary |
---|---|
private static final String uiClassID Hides javax.
|
JProgressBar | back to summary |
---|---|
public JProgressBar() Creates a horizontal progress bar that displays a border but no progress string. The initial and minimum values are 0, and the maximum is 100. |
JProgressBar | back to summary |
---|---|
public JProgressBar(int orient) Creates a progress bar with the specified orientation,
which can be
either
|
JProgressBar | back to summary |
---|---|
public JProgressBar(int min, int max) Creates a horizontal progress bar with the specified minimum and maximum. Sets the initial value of the progress bar to the specified minimum. By default, a border is painted but a progress string is not.
The
|
JProgressBar | back to summary |
---|---|
public JProgressBar(int orient, int min, int max) Creates a progress bar using the specified orientation, minimum, and maximum. By default, a border is painted but a progress string is not. Sets the initial value of the progress bar to the specified minimum.
The
|
JProgressBar | back to summary |
---|---|
public JProgressBar(BoundedRangeModel newModel) Creates a horizontal progress bar that uses the specified model to hold the progress bar's data. By default, a border is painted but a progress string is not.
|
addChangeListener | back to summary |
---|---|
public void addChangeListener(ChangeListener l) Adds the specified
|
createChangeListener | back to summary |
---|---|
protected ChangeListener createChangeListener() Subclasses that want to handle change events
from the model differently
can override this to return
an instance of a custom
|
fireStateChanged | back to summary |
---|---|
protected void fireStateChanged() Send a
The event instance is created if necessary, and stored in
|
getAccessibleContext | back to summary |
---|---|
public AccessibleContext getAccessibleContext() Overrides java. Implements javax. Gets the
|
getChangeListeners | back to summary |
---|---|
public ChangeListener[] getChangeListeners() Returns an array of all the
|
getMaximum | back to summary |
---|---|
public int getMaximum() Returns the progress bar's
|
getMinimum | back to summary |
---|---|
public int getMinimum() Returns the progress bar's
|
getModel | back to summary |
---|---|
public BoundedRangeModel getModel() Returns the data model used by this progress bar.
|
getOrientation | back to summary |
---|---|
public int getOrientation() Returns
|
getPercentComplete | back to summary |
---|---|
public double getPercentComplete() Returns the percent complete for the progress bar. Note that this number is between 0.0 and 1.0.
|
getString | back to summary |
---|---|
public String getString() Returns a |
getUI | back to summary |
---|---|
public ProgressBarUI getUI() Overrides javax. Returns the look-and-feel object that renders this component.
|
getUIClassID | back to summary |
---|---|
public String getUIClassID() Overrides javax. Returns the name of the look-and-feel class that renders this component.
|
getValue | back to summary |
---|---|
public int getValue() Returns the progress bar's current
|
isBorderPainted | back to summary |
---|---|
public boolean isBorderPainted() Returns the
|
isIndeterminate | back to summary |
---|---|
public boolean isIndeterminate() Returns the value of the
|
isStringPainted | back to summary |
---|---|
public boolean isStringPainted() Returns the value of the
|
paintBorder | back to summary |
---|---|
protected void paintBorder(Graphics g) Overrides javax. Paints the progress bar's border if the
|
paramString | back to summary |
---|---|
protected String paramString() Overrides javax. Returns a string representation of this
|
removeChangeListener | back to summary |
---|---|
public void removeChangeListener(ChangeListener l) Removes a
|
setBorderPainted | back to summary |
---|---|
public void setBorderPainted(boolean b) Sets the
|
setIndeterminate | back to summary |
---|---|
public void setIndeterminate(boolean newValue) Sets the See How to Monitor Progress for examples of using indeterminate progress bars.
|
setMaximum | back to summary |
---|---|
public void setMaximum(int n) Sets the progress bar's maximum value
(stored in the progress bar's data model) to
The underlying If the maximum value is different from the previous maximum, all change listeners are notified.
|
setMinimum | back to summary |
---|---|
public void setMinimum(int n) Sets the progress bar's minimum value
(stored in the progress bar's data model) to
The data model (a If the minimum value is different from the previous minimum, all change listeners are notified.
|
setModel | back to summary |
---|---|
public void setModel(BoundedRangeModel newModel) Sets the data model used by the
|
setOrientation | back to summary |
---|---|
public void setOrientation(int newOrientation) Sets the progress bar's orientation to
|
setString | back to summary |
---|---|
public void setString(String s) Sets the value of the progress string. By default,
this string is
The progress string is painted only if
the
|
setStringPainted | back to summary |
---|---|
public void setStringPainted(boolean b) Sets the value of the
|
setUI | back to summary |
---|---|
public void setUI(ProgressBarUI ui) Sets the look-and-feel object that renders this component.
|
setValue | back to summary |
---|---|
public void setValue(int n) Sets the progress bar's current value to
The data model (an instance of If the new value is different from the previous value, all change listeners are notified.
|
updateUI | back to summary |
---|---|
public void updateUI() Overrides javax. Resets the UI property to a value from the current look and feel.
|
writeObject | back to summary |
---|---|
private void writeObject(ObjectOutputStream s) throws IOException Hides javax. See readObject() and writeObject() in JComponent for more information about serialization in Swing.
|
JProgressBar
class. It provides an implementation of the
Java Accessibility API appropriate to progress bar user-interface
elements.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 java.
.
Access | Constructor and Description |
---|---|
protected |
Modifier and Type | Method and Description |
---|---|
public AccessibleRole | Returns: an instance of AccessibleRole describing the role of the objectOverrides javax. |
public AccessibleStateSet | Returns: an instance of AccessibleState containing the current state of the objectOverrides javax. |
public AccessibleValue | Returns: this objectOverrides javax. AccessibleValue associated with this object.
|
public Number | Returns: the current value of this objectImplements javax. |
public Number | Returns: the maximum value of this objectImplements javax. |
public Number | Returns: the minimum value of this objectImplements javax. |
public boolean | Returns: true if the value was setthe number to use for the value n)Implements javax. Number .
|
AccessibleJProgressBar | back to summary |
---|---|
protected AccessibleJProgressBar() Constructs an |
getAccessibleRole | back to summary |
---|---|
public AccessibleRole getAccessibleRole() Overrides javax. Gets the role of this object.
|
getAccessibleStateSet | back to summary |
---|---|
public AccessibleStateSet getAccessibleStateSet() Overrides javax. Gets the state set of this object.
|
getAccessibleValue | back to summary |
---|---|
public AccessibleValue getAccessibleValue() Overrides javax. Gets the
|
getCurrentAccessibleValue | back to summary |
---|---|
public Number getCurrentAccessibleValue() Implements javax. Gets the accessible value of this object.
|
getMaximumAccessibleValue | back to summary |
---|---|
public Number getMaximumAccessibleValue() Implements javax. Gets the maximum accessible value of this object.
|
getMinimumAccessibleValue | back to summary |
---|---|
public Number getMinimumAccessibleValue() Implements javax. Gets the minimum accessible value of this object.
|
setCurrentAccessibleValue | back to summary |
---|---|
public boolean setCurrentAccessibleValue(Number n) Implements javax. Sets the value of this object as a
|
Access | Constructor and Description |
---|---|
private |
Modifier and Type | Method and Description |
---|---|
public void | stateChanged(ChangeEvent
a ChangeEvent object e)Implements javax. |
ModelListener | back to summary |
---|---|
private ModelListener() |
stateChanged | back to summary |
---|---|
public void stateChanged(ChangeEvent e) Implements javax. Doc from javax. Invoked when the target of the listener has changed its state.
|