javax.swing

Class JProgressBar

Implemented Interfaces:
Accessible, ImageObserver, MenuContainer, Serializable, 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

TOOL_TIP_TEXT_KEY, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW, accessibleContext, listenerList, ui

Fields inherited from class java.awt.Component

BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT

Fields inherited from interface java.awt.image.ImageObserver

ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH

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

Constructor Summary

JProgressBar()
Creates a new JProgressBar with default attributes.
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.
JProgressBar(BoundedRangeModel model)
Creates a new JProgressBar with the specified model.

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, extends EventListener> T[] getListeners, firePropertyChange, firePropertyChange, fireVetoableChange, getAccessibleContext, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getUIClassID, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, paramString, 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, updateUI

Methods inherited from class java.awt.Container

add, add, add, add, add, addContainerListener, addImpl, addNotify, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, extends EventListener> T[] getListeners, findComponentAt, findComponentAt, getAlignmentX, getAlignmentY, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponentZOrder, getComponents, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getInsets, getLayout, getMaximumSize, getMinimumSize, getPreferredSize, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paint, paintComponents, paramString, preferredSize, print, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, removeNotify, setComponentZOrder, setFocusCycleRoot, setFocusTraversalKeys, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setFont, setLayout, transferFocusDownCycle, update, validate, validateTree

Methods inherited from class java.awt.Component

action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, addNotify, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, createVolatileImage, createVolatileImage, deliverEvent, disable, disableEvents, dispatchEvent, doLayout, enable, enable, enableEvents, enableInputMethods, extends EventListener> T[] getListeners, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getAccessibleContext, getAlignmentX, getAlignmentY, getBackground, getBounds, getBounds, getColorModel, getComponentAt, getComponentAt, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeys, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphics, getGraphicsConfiguration, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocation, getLocationOnScreen, getMaximumSize, getMinimumSize, getMouseListeners, getMouseMotionListeners, getMouseWheelListeners, getName, getParent, getPeer, getPreferredSize, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getToolkit, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, invalidate, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusCycleRoot, isFocusOwner, isFocusTraversable, isFocusable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, layout, list, list, list, list, list, locate, location, lostFocus, minimumSize, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paint, paintAll, paramString, postEvent, preferredSize, prepareImage, prepareImage, print, printAll, processComponentEvent, processEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, reshape, resize, resize, setBackground, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFocusTraversalKeys, setFocusTraversalKeysEnabled, setFocusable, setFont, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setMaximumSize, setMinimumSize, setName, setPreferredSize, setSize, setSize, setVisible, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle, update, validate

Methods inherited from class java.lang.Object

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Field Details

changeEvent

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

changeListener

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

model

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

orientation

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

paintBorder

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

paintString

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

progressString

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

Constructor Details

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:

  • value: 0;
  • minimum: 0;
  • maximum: 100;
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:

  • value: minimum;
Parameters:
orientation - the orientation (SwingConstants.HORIZONTAL or SwingConstants.VERTICAL).
minimum - the lower bound of the value range.
maximum - the upper bound of the value range.
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 Details

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.

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.

fireStateChanged

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

getAccessibleContext

public AccessibleContext getAccessibleContext()
Returns the object that provides accessibility features for this JProgressBar component.
Specified by:
getAccessibleContext in interface Accessible
Overrides:
getAccessibleContext in interface JComponent
Returns:
The accessible context (an instance of JProgressBar.AccessibleJProgressBar).

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

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.

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.

getModel

public BoundedRangeModel getModel()
Returns the model for the JProgressBar.
Returns:
The model (never null).

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.

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).

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.

getUI

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

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 interface JComponent
Returns:
"ProgressBarUI".

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)

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.

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

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.

paintBorder

protected void paintBorder(Graphics graphics)
Paints the component's border, but only if isBorderPainted() returns true.
Overrides:
paintBorder in interface JComponent
Parameters:
graphics - the graphics object to paint with.

paramString

protected String paramString()
Returns an implementation-dependent string describing the attributes of this JProgressBar.
Overrides:
paramString in interface JComponent
Returns:
A string describing the attributes of this JProgressBar (never null).

removeChangeListener

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

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.

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

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()

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()

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()

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.

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).

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.

setUI

public void setUI(ProgressBarUI ui)
Sets the UI delegate for this component.
Parameters:
ui - the new UI delegate.

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()

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 interface JComponent

JProgressBar.java -- Copyright (C) 2002, 2004, 2005, 2006 Free Software Foundation, Inc. This file is part of GNU Classpath. GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GNU Classpath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Classpath; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination. As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version.