javax.swing
Class JProgressBar

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by javax.swing.JComponent
              extended by javax.swing.JProgressBar
All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable, Accessible, SwingConstants

public class JProgressBar
extends JComponent
implements SwingConstants, Accessible

A component that displays a visual indicator of the progress of a task. The component has two modes: determinate and indeterminate. In determinate mode, the JProgressBar fills a percentage of its bar based on its current value. In indeterminate mode, it creates box and bounces it between its bounds.

This component has the following properties:

Property Stored in Bound?
borderPainted progressBar yes
changeListeners progressBar no
indeterminate progressBar yes
maximum model no
minimum model no
model progressBar no
orientation progressBar yes
percentComplete progressBar no
string progressBar yes
stringPainted progressBar yes
value model no

See Also:
Serialized Form

Nested Class Summary
protected  class JProgressBar.AccessibleJProgressBar
          Provides the accessibility features for the JProgressBar component.
 
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.BltBufferStrategy, Component.FlipBufferStrategy
 
Field Summary
protected  ChangeEvent changeEvent
          A single change event reused for all events.
protected  ChangeListener changeListener
          The listener that is registered with the model.
protected  BoundedRangeModel model
          The model defining the bounds and current value for the progress bar.
protected  int orientation
          The orientation of the JProgressBar (SwingConstants.HORIZONTAL or SwingConstants.VERTICAL).
protected  boolean paintBorder
          A flag the controls whether or not the component's border is painted.
protected  boolean paintString
          A flag that controls whether a string is displayed within the progress bar.
protected  String progressString
          A custom string for display in 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 new JProgressBar with default attributes.
JProgressBar(BoundedRangeModel model)
          Creates a new JProgressBar with the specified model.
JProgressBar(int orientation)
          Creates a new JProgressBar with the specified orientation.
JProgressBar(int minimum, int maximum)
          Creates a new JProgressBar with the specified value range.
JProgressBar(int orientation, int minimum, int maximum)
          Creates a new JProgressBar with the specified range and orientation.
 
Method Summary
 void addChangeListener(ChangeListener listener)
          Registers a listener with this component so that it will receive notification of component state changes.
protected  ChangeListener createChangeListener()
          Creates a new ChangeListener that calls fireStateChanged() whenever it receives a ChangeEvent (typically from the component's model).
protected  void fireStateChanged()
          Sends a ChangeEvent to all registered listeners to indicate that the state of the JProgressBar has changed.
 AccessibleContext getAccessibleContext()
          Returns the object that provides accessibility features for this JProgressBar component.
 ChangeListener[] getChangeListeners()
          Returns an array of the listeners that are registered with this component.
 int getMaximum()
          Returns the maximum value for the JProgressBar.
 int getMinimum()
          Returns the minimum value for the JProgressBar.
 BoundedRangeModel getModel()
          Returns the model for the JProgressBar.
 int getOrientation()
          Returns the orientation of the JProgressBar component, which is either SwingConstants.HORIZONTAL or SwingConstants.VERTICAL.
 double getPercentComplete()
          Returns the current value expressed as a percentage.
 String getString()
          Returns the string that is painted on the JProgressBar if isStringPainted() returns true.
 ProgressBarUI getUI()
          Returns the UI delegate for this JProgressBar.
 String getUIClassID()
          Returns the suffix ("ProgressBarUI" in this case) used to determine the class name for a UI delegate that can provide the look and feel for a JProgressBar.
 int getValue()
          Returns the current value for the JProgressBar.
 boolean isBorderPainted()
          Returns a flag that controls whether or not the component's border is painted.
 boolean isIndeterminate()
          Returns a flag that indicates the mode for this JProgressBar (true for indeterminate mode, and false for determinate mode).
 boolean isStringPainted()
          Returns the flag that controls whether or not the string returned by getString() is displayed by the JProgressBar component.
protected  void paintBorder(Graphics graphics)
          Paints the component's border, but only if isBorderPainted() returns true.
protected  String paramString()
          Returns an implementation-dependent string describing the attributes of this JProgressBar.
 void removeChangeListener(ChangeListener listener)
          Deregisters a listener so that it no longer receives notification of component state changes.
 void setBorderPainted(boolean painted)
          Sets the flag that controls whether or not the component's border is painted.
 void setIndeterminate(boolean flag)
          Sets the flag that controls the mode for this JProgressBar (true for indeterminate mode, and false for determinate mode).
 void setMaximum(int maximum)
          Sets the maximum value for the JProgressBar.
 void setMinimum(int minimum)
          Sets the minimum value for the JProgressBar.
 void setModel(BoundedRangeModel model)
          Sets the model for the JProgressBar and sends a ChangeEvent to all registered listeners.
 void setOrientation(int orientation)
          Sets the orientation for this JProgressBar component and, if the value changes, sends a PropertyChangeEvent (with the property name "orientation") to all registered listeners.
 void setString(String string)
          Sets the string to display within the progress bar and, if the new value is different to the old value, sends a PropertyChangeEvent (with the property name "string") to all registered listeners.
 void setStringPainted(boolean painted)
          Sets the flag that controls whether or not the string returned by getString() is displayed by the JProgressBar component.
 void setUI(ProgressBarUI ui)
          Sets the UI delegate for this component.
 void setValue(int value)
          Sets the current value for the JProgressBar.
 void updateUI()
          Sets this JProgressBar's UI delegate to the default (obtained from the UIManager) for the current look and feel.
 
Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintChildren, paintComponent, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, 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, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setNextFocusableComponent, setOpaque, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
 
Methods inherited from class java.awt.Container
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, setFocusTraversalKeys, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusDownCycle, validate, validateTree
 
Methods inherited from class java.awt.Component
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, getFontMetrics, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, hide, 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, processMouseEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setMaximumSize, setMinimumSize, setName, setPreferredSize, setSize, setSize, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

orientation

protected int orientation
The orientation of the JProgressBar (SwingConstants.HORIZONTAL or SwingConstants.VERTICAL). Defaults to SwingConstants.HORIZONTAL.

See Also:
setOrientation(int)

paintBorder

protected boolean paintBorder
A flag the controls whether or not the component's border is painted. The default is true.

See Also:
setBorderPainted(boolean)

model

protected BoundedRangeModel model
The model defining the bounds and current value for the progress bar.

See Also:
setModel(BoundedRangeModel)

progressString

protected String progressString
A custom string for display in the progress bar. If this is null, a default string will be generated.

See Also:
setString(String)

paintString

protected boolean paintString
A flag that controls whether a string is displayed within the progress bar.

See Also:
setStringPainted(boolean)

changeEvent

protected transient ChangeEvent changeEvent
A single change event reused for all events.

See Also:
fireStateChanged()

changeListener

protected ChangeListener changeListener
The listener that is registered with the model.

Constructor Detail

JProgressBar

public JProgressBar()
Creates a new JProgressBar with default attributes. The following defaults are used:


JProgressBar

public JProgressBar(int orientation)
Creates a new JProgressBar with the specified orientation. The following defaults are used:

Parameters:
orientation - the orientation (SwingConstants.HORIZONTAL or SwingConstants.VERTICAL).
Throws:
IllegalArgumentException - if orientation is not one of the specified values.

JProgressBar

public JProgressBar(int minimum,
                    int maximum)
Creates a new JProgressBar with the specified value range. The following defaults are used:

Parameters:
minimum - the lower bound of the value range.
maximum - the upper bound of the value range.

JProgressBar

public JProgressBar(int orientation,
                    int minimum,
                    int maximum)
Creates a new JProgressBar with the specified range and orientation. The following defaults are used:

Parameters:
minimum - the lower bound of the value range.
maximum - the upper bound of the value range.
orientation - the orientation (SwingConstants.HORIZONTAL or SwingConstants.VERTICAL).
Throws:
IllegalArgumentException - if orientation is not one of the specified values.

JProgressBar

public JProgressBar(BoundedRangeModel model)
Creates a new JProgressBar with the specified model. The following defaults are used:

Parameters:
model - the model (null not permitted).
Method Detail

getValue

public int getValue()
Returns the current value for the JProgressBar. This value is fetched from the model.

Returns:
The current value.
See Also:
setValue(int)

setValue

public void setValue(int value)
Sets the current value for the JProgressBar. The value is stored in the component's model (see getModel()). If the new value is different to the old value, a ChangeEvent is sent to the model's registered listeners. In turn, this triggers a call to fireStateChanged() which will send a ChangeEvent to this component's registered listeners.

If value is outside the range minimum to maximum, it will be set to the nearest of those boundary values.

Parameters:
value - the new value.
See Also:
getValue()

paintBorder

protected void paintBorder(Graphics graphics)
Paints the component's border, but only if isBorderPainted() returns true.

Overrides:
paintBorder in class JComponent
Parameters:
graphics - the graphics object to paint with.
See Also:
setBorderPainted(boolean)

getOrientation

public int getOrientation()
Returns the orientation of the JProgressBar component, which is either SwingConstants.HORIZONTAL or SwingConstants.VERTICAL. The default orientation is HORIZONTAL.

Returns:
The orientation.
See Also:
setOrientation(int)

setOrientation

public void setOrientation(int orientation)
Sets the orientation for this JProgressBar component and, if the value changes, sends a PropertyChangeEvent (with the property name "orientation") to all registered listeners.

Parameters:
orientation - the orientation (SwingConstants.HORIZONTAL or SwingConstants.VERTICAL).
Throws:
IllegalArgumentException - if orientation is not one of the listed values.
See Also:
getOrientation()

isStringPainted

public boolean isStringPainted()
Returns the flag that controls whether or not the string returned by getString() is displayed by the JProgressBar component.

Returns:
true if the string should be displayed, and false otherwise.
See Also:
setStringPainted(boolean)

setStringPainted

public void setStringPainted(boolean painted)
Sets the flag that controls whether or not the string returned by getString() is displayed by the JProgressBar component. If the flag value changes, a PropertyChangeEvent (with the property name "stringPainted") is sent to all registered listeners.

Parameters:
painted - the new flag value.
See Also:
isStringPainted(), setString(String)

getString

public String getString()
Returns the string that is painted on the JProgressBar if isStringPainted() returns true. If no string has been explicitly set, this method will return a string displaying the value of getPercentComplete().

Returns:
The string.
See Also:
setString(String), setStringPainted(boolean)

setString

public void setString(String string)
Sets the string to display within the progress bar and, if the new value is different to the old value, sends a PropertyChangeEvent (with the property name "string") to all registered listeners. If the string is set to null, getString() will return a default string.

Parameters:
string - the string (null permitted).
See Also:
getString(), setStringPainted(boolean)

getPercentComplete

public double getPercentComplete()
Returns the current value expressed as a percentage. This is calculated as (value - min) / (max - min).

Returns:
The percentage (a value in the range 0.0 to 1.0).

isBorderPainted

public boolean isBorderPainted()
Returns a flag that controls whether or not the component's border is painted. The default value is true.

Returns:
true if the component's border should be painted, and false otherwise.
See Also:
setBorderPainted(boolean)

setBorderPainted

public void setBorderPainted(boolean painted)
Sets the flag that controls whether or not the component's border is painted. If the flag value is changed, this method sends a PropertyChangeEvent (with the property name "borderPainted") to all registered listeners.

Parameters:
painted - the new flag value.
See Also:
isBorderPainted(), paintBorder

getUI

public ProgressBarUI getUI()
Returns the UI delegate for this JProgressBar.

Returns:
The UI delegate.

setUI

public void setUI(ProgressBarUI ui)
Sets the UI delegate for this component.

Parameters:
ui - the new UI delegate.

updateUI

public void updateUI()
Sets this JProgressBar's UI delegate to the default (obtained from the UIManager) for the current look and feel.

Overrides:
updateUI in class JComponent

getUIClassID

public String getUIClassID()
Returns the suffix ("ProgressBarUI" in this case) used to determine the class name for a UI delegate that can provide the look and feel for a JProgressBar.

Overrides:
getUIClassID in class JComponent
Returns:
"ProgressBarUI".
See Also:
JComponent.setUI(javax.swing.plaf.ComponentUI), JComponent.updateUI()

createChangeListener

protected ChangeListener createChangeListener()
Creates a new ChangeListener that calls fireStateChanged() whenever it receives a ChangeEvent (typically from the component's model). This listener is registered with the progress bar's model, so that changes made to the model directly will automatically result in the progress bar's listeners being notified also.

Returns:
A new listener.

addChangeListener

public void addChangeListener(ChangeListener listener)
Registers a listener with this component so that it will receive notification of component state changes.

Parameters:
listener - the listener.
See Also:
removeChangeListener(ChangeListener)

removeChangeListener

public void removeChangeListener(ChangeListener listener)
Deregisters a listener so that it no longer receives notification of component state changes.

Parameters:
listener - the listener.
See Also:
addChangeListener(ChangeListener)

getChangeListeners

public ChangeListener[] getChangeListeners()
Returns an array of the listeners that are registered with this component. The array may be empty, but is never null.

Returns:
An array of listeners.
Since:
1.4

fireStateChanged

protected void fireStateChanged()
Sends a ChangeEvent to all registered listeners to indicate that the state of the JProgressBar has changed.

See Also:
createChangeListener()

getModel

public BoundedRangeModel getModel()
Returns the model for the JProgressBar.

Returns:
The model (never null).
See Also:
setModel(BoundedRangeModel)

setModel

public void setModel(BoundedRangeModel model)
Sets the model for the JProgressBar and sends a ChangeEvent to all registered listeners.

Parameters:
model - the model (null not permitted).
See Also:
getModel()

getMinimum

public int getMinimum()
Returns the minimum value for the JProgressBar. This defines the lower bound for the current value, and is stored in the component's model.

Returns:
The minimum value.
See Also:
setMinimum(int)

setMinimum

public void setMinimum(int minimum)
Sets the minimum value for the JProgressBar. The value is stored in the component's model (see getModel()). If the new value is different to the old value, a ChangeEvent is sent to the model's registered listeners. In turn, this triggers a call to fireStateChanged() which will send a ChangeEvent to this component's registered listeners.

Parameters:
minimum - the minimum value.
See Also:
getMinimum()

getMaximum

public int getMaximum()
Returns the maximum value for the JProgressBar. This defines the upper bound for the current value, and is stored in the component's model.

Returns:
The maximum value.
See Also:
setMaximum(int)

setMaximum

public void setMaximum(int maximum)
Sets the maximum value for the JProgressBar. The value is stored in the component's model (see getModel()). If the new value is different to the old value, a ChangeEvent is sent to the model's registered listeners. In turn, this triggers a call to fireStateChanged() which will send a ChangeEvent to this component's registered listeners.

Parameters:
maximum - the maximum value.
See Also:
getMaximum()

paramString

protected String paramString()
Returns an implementation-dependent string describing the attributes of this JProgressBar.

Overrides:
paramString in class JComponent
Returns:
A string describing the attributes of this JProgressBar (never null).

setIndeterminate

public void setIndeterminate(boolean flag)
Sets the flag that controls the mode for this JProgressBar (true for indeterminate mode, and false for determinate mode). If the flag value changes, this method sends a PropertyChangeEvent (with the property name "indeterminate") to all registered listeners.

If the JProgressBar is determinate, it paints a percentage of the bar described by its value. If it is indeterminate, it simply bounces a box between the ends of the bar; the value of the JProgressBar is ignored.

Parameters:
flag - the new flag value.
Since:
1.4
See Also:
isIndeterminate()

isIndeterminate

public boolean isIndeterminate()
Returns a flag that indicates the mode for this JProgressBar (true for indeterminate mode, and false for determinate mode).

Returns:
A flag indicating the mode for the JProgressBar.
Since:
1.4
See Also:
setIndeterminate(boolean)

getAccessibleContext

public AccessibleContext getAccessibleContext()
Returns the object that provides accessibility features for this JProgressBar component.

Specified by:
getAccessibleContext in interface Accessible
Overrides:
getAccessibleContext in class JComponent
Returns:
The accessible context (an instance of JProgressBar.AccessibleJProgressBar).