ImageObserver
, MenuContainer
, Serializable
, Accessible
, SwingConstants
@JavaBean(defaultProperty="UI", description="A component that displays an integer value.") 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
.
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
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
BOTTOM, CENTER, EAST, HORIZONTAL, LEADING, LEFT, NEXT, NORTH, NORTH_EAST, NORTH_WEST, PREVIOUS, RIGHT, SOUTH, SOUTH_EAST, SOUTH_WEST, TOP, TRAILING, VERTICAL, WEST
Constructor | Description |
---|---|
JProgressBar() |
Creates a horizontal progress bar that displays a border but no progress string. |
JProgressBar |
Creates a progress bar with the specified orientation, which can be either SwingConstants.VERTICAL or SwingConstants.HORIZONTAL . |
JProgressBar |
Creates a horizontal progress bar with the specified minimum and maximum. |
JProgressBar |
Creates a progress bar using the specified orientation, minimum, and maximum. |
JProgressBar |
Creates a horizontal progress bar that uses the specified model to hold the progress bar's data. |
Modifier and Type | Method | Description |
---|---|---|
void |
addChangeListener |
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[] |
getChangeListeners() |
Returns an array of all the ChangeListener s added to this progress bar with addChangeListener . |
int |
getMaximum() |
Returns the progress bar's maximum value from the BoundedRangeModel . |
int |
getMinimum() |
Returns the progress bar's minimum value from the BoundedRangeModel . |
BoundedRangeModel |
getModel() |
Returns the data model used by this progress bar. |
int |
getOrientation() |
Returns SwingConstants.VERTICAL or SwingConstants.HORIZONTAL , depending on the orientation of the progress bar. |
double |
getPercentComplete() |
Returns the percent complete for the progress bar. |
String |
getString() |
Returns a String representation of the 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 the BoundedRangeModel . |
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 |
Paints the progress bar's border if the borderPainted property is true . |
protected String |
paramString() |
Returns a string representation of this JProgressBar . |
void |
removeChangeListener |
Removes a ChangeListener from the progress bar. |
void |
setBorderPainted |
Sets the borderPainted property, which is true if the progress bar should paint its border. |
void |
setIndeterminate |
Sets the indeterminate property of the progress bar, which determines whether the progress bar is in determinate or indeterminate mode. |
void |
setMaximum |
Sets the progress bar's maximum value (stored in the progress bar's data model) to n . |
void |
setMinimum |
Sets the progress bar's minimum value (stored in the progress bar's data model) to n . |
void |
setModel |
Sets the data model used by the JProgressBar . |
void |
setOrientation |
Sets the progress bar's orientation to newOrientation , which must be SwingConstants.VERTICAL or SwingConstants.HORIZONTAL . |
void |
setString |
Sets the value of the progress string. |
void |
setStringPainted |
Sets the value of the stringPainted property, which determines whether the progress bar should render a progress string. |
void |
setUI |
Sets the look-and-feel object that renders this component. |
void |
setValue |
Sets the progress bar's current value to n . |
void |
updateUI() |
Resets the UI property to a value from the current look and feel. |
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBaseline, getBaselineResizeBehavior, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, hide, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingOrigin, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintChildren, paintComponent, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
add, add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusDownCycle, validate, validateTree
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setMixingCutoutShape, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
protected int orientation
HORIZONTAL
.protected boolean paintBorder
true
.protected BoundedRangeModel model
protected String progressString
null
. Setting this to a non-null
value does not imply that the string will be displayed. To display the string, paintString
must be true
.protected boolean paintString
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.protected transient ChangeEvent changeEvent
ChangeEvent
is needed per instance since the event's only interesting property is the immutable source, which is the progress bar. The event is lazily created the first time that an event notification is fired.protected ChangeListener changeListener
public JProgressBar()
public JProgressBar(int orient)
SwingConstants.VERTICAL
or SwingConstants.HORIZONTAL
. By default, a border is painted but a progress string is not. The initial and minimum values are 0, and the maximum is 100.orient
- the desired orientation of the progress barIllegalArgumentException
- if orient
is an illegal valuepublic 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.
min
- the minimum value of the progress barmax
- the maximum value of the progress barpublic 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.
orient
- the desired orientation of the progress barmin
- the minimum value of the progress barmax
- the maximum value of the progress barIllegalArgumentException
- if orient
is an illegal valuepublic JProgressBar(BoundedRangeModel newModel)
newModel
- the data model for the progress barpublic int getOrientation()
SwingConstants.VERTICAL
or SwingConstants.HORIZONTAL
, depending on the orientation of the progress bar. The default orientation is SwingConstants.HORIZONTAL
.HORIZONTAL
or VERTICAL
@BeanProperty(preferred=true, visualUpdate=true, description="Set the progress bar\'s orientation.") public void setOrientation(int newOrientation)
newOrientation
, which must be SwingConstants.VERTICAL
or SwingConstants.HORIZONTAL
. The default orientation is SwingConstants.HORIZONTAL
.newOrientation
- HORIZONTAL
or VERTICAL
IllegalArgumentException
- if newOrientation
is an illegal valuepublic boolean isStringPainted()
stringPainted
property.stringPainted
property@BeanProperty(visualUpdate=true, description="Whether the progress bar should render a string.") public void setStringPainted(boolean b)
stringPainted
property, which determines whether the progress bar should render a progress string. The default is false
, meaning no string is painted. Some look and feels might not support progress strings or might support them only when the progress bar is in determinate mode.b
- true
if the progress bar should render a stringpublic String getString()
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
.null
@BeanProperty(visualUpdate=true, description="Specifies the progress string to paint") public void setString(String s)
null
, implying the built-in behavior of using a simple percent string. If you have provided a custom progress string and want to revert to the built-in behavior, set the string back to null
. The progress string is painted only if the isStringPainted
method returns true
.
s
- the value of the progress string@BeanProperty(bound=false) public double getPercentComplete()
public boolean isBorderPainted()
borderPainted
property.borderPainted
property@BeanProperty(visualUpdate=true, description="Whether the progress bar should paint its border.") public void setBorderPainted(boolean b)
borderPainted
property, which is true
if the progress bar should paint its border. The default value for this property is true
. Some look and feels might not implement painted borders; they will ignore this property.b
- true
if the progress bar should paint its border; otherwise, false
protected void paintBorder(Graphics g)
borderPainted
property is true
.paintBorder
in class JComponent
g
- the Graphics
context within which to paint the borderpublic ProgressBarUI getUI()
getUI
in class JComponent
ProgressBarUI
object that renders this component@BeanProperty(hidden=true, visualUpdate=true, description="The UI object that implements the Component\'s LookAndFeel.") public void setUI(ProgressBarUI ui)
ui
- a ProgressBarUI
objectpublic void updateUI()
updateUI
in class JComponent
@BeanProperty(bound=false, expert=true, description="A string that specifies the name of the look-and-feel class.") public String getUIClassID()
getUIClassID
in class JComponent
protected ChangeListener createChangeListener()
ChangeListener
implementation. The default ChangeListener
simply calls the fireStateChanged
method to forward ChangeEvent
s to the ChangeListener
s that have been added directly to the progress bar.ChangeListener
implementation.public void addChangeListener(ChangeListener l)
ChangeListener
to the progress bar.l
- the ChangeListener
to addpublic void removeChangeListener(ChangeListener l)
ChangeListener
from the progress bar.l
- the ChangeListener
to remove@BeanProperty(bound=false) public ChangeListener[] getChangeListeners()
ChangeListener
s added to this progress bar with addChangeListener
.ChangeListener
s added or an empty array if no listeners have been addedprotected void fireStateChanged()
ChangeEvent
, whose source is this JProgressBar
, to all ChangeListener
s that have registered interest in ChangeEvent
s. This method is called each time a ChangeEvent
is received from the model. The event instance is created if necessary, and stored in changeEvent
.
public BoundedRangeModel getModel()
BoundedRangeModel
currently in use@BeanProperty(bound=false, expert=true, description="The data model used by the JProgressBar.") public void setModel(BoundedRangeModel newModel)
JProgressBar
. Note that the BoundedRangeModel
's extent
is not used, and is set to 0
.newModel
- the BoundedRangeModel
to usepublic int getValue()
value
from the BoundedRangeModel
. The value is always between the minimum and maximum values, inclusive.public int getMinimum()
minimum
value from the BoundedRangeModel
.public int getMaximum()
maximum
value from the BoundedRangeModel
.@BeanProperty(bound=false, preferred=true, description="The progress bar\'s current value.") public void setValue(int n)
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.
n
- the new value@BeanProperty(bound=false, preferred=true, description="The progress bar\'s minimum value.") public void setMinimum(int n)
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.
n
- the new minimum@BeanProperty(bound=false, preferred=true, description="The progress bar\'s maximum value.") public void setMaximum(int n)
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.
n
- the new maximumpublic void setIndeterminate(boolean newValue)
indeterminate
property of the progress bar, which determines whether the progress bar is in determinate or indeterminate mode. An indeterminate progress bar continuously displays animation indicating that an operation of unknown length is occurring. By default, this property is false
. Some look and feels might not support indeterminate progress bars; they will ignore this property. See How to Monitor Progress for examples of using indeterminate progress bars.
newValue
- true
if the progress bar should change to indeterminate mode; false
if it should revert to normal.@BeanProperty(bound=false, description="Is the progress bar indeterminate (true) or normal (false)?") public boolean isIndeterminate()
indeterminate
property.indeterminate
propertyprotected String paramString()
JProgressBar
. This method is intended to be used only for debugging purposes. The content and format of the returned string may vary between implementations. The returned string may be empty but may not be null
.paramString
in class JComponent
JProgressBar
@BeanProperty(bound=false, expert=true, description="The AccessibleContext associated with this ProgressBar.") public AccessibleContext getAccessibleContext()
AccessibleContext
associated with this JProgressBar
. For progress bars, the AccessibleContext
takes the form of an AccessibleJProgressBar
. A new AccessibleJProgressBar
instance is created if necessary.getAccessibleContext
in interface Accessible
getAccessibleContext
in class Component
AccessibleJProgressBar
that serves as the AccessibleContext
of this JProgressBar
© 1993, 2023, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
https://docs.oracle.com/en/java/javase/21/docs/api/java.desktop/javax/swing/JProgressBar.html