Module java.desktop
Package javax.swing

Class JProgressBar

  • All Implemented Interfaces:
    ImageObserver, MenuContainer, Serializable, Accessible, SwingConstants

              description="A component that displays an integer value.")
    public class JProgressBar
    extends JComponent
    implements SwingConstants, Accessible
    A component that visually displays the progress of some task. As the task progresses towards completion, the progress bar displays the task's percentage of completion. This percentage is typically represented visually by a rectangle which starts out empty and gradually becomes filled in as the task progresses. In addition, the progress bar can display a textual representation of this percentage.

    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());
    Here is an example of querying the current state of the task, and using the returned value to update the progress bar:
    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:
    ...//do some work; get length of task...

    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.

    See Also:
    BasicProgressBarUI, BoundedRangeModel, SwingWorker, Serialized Form
    • Field Detail

      • orientation

        protected int orientation
        Whether the progress bar is horizontal or vertical. The default is HORIZONTAL.
        See Also:
      • paintBorder

        protected boolean paintBorder
        Whether to display a border around the progress bar. The default is true.
        See Also:
      • progressString

        protected String progressString
        An optional string that can be displayed on the progress bar. The default is 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.
        See Also:
        setString(java.lang.String), setStringPainted(boolean)
      • paintString

        protected boolean paintString
        Whether to display a string of text on the progress bar. The default is false. Setting this to true causes a textual display of the progress to be rendered on the progress bar. If the progressString is null, the percentage of completion is displayed on the progress bar. Otherwise, the progressString is rendered on the progress bar.
        See Also:
        setStringPainted(boolean), setString(java.lang.String)
      • changeEvent

        protected transient 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. The event is lazily created the first time that an event notification is fired.
        See Also:
      • changeListener

        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.
        See Also:
    • Method Detail

      • getOrientation

        public int getOrientation​()
        Returns SwingConstants.VERTICAL or SwingConstants.HORIZONTAL, depending on the orientation of the progress bar. The default orientation is SwingConstants.HORIZONTAL.
        See Also:
      • setOrientation

                      description="Set the progress bar\'s orientation.")
        public void setOrientation​(int newOrientation)
        Sets the progress bar's orientation to 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 value
        See Also:
      • setStringPainted

                      description="Whether the progress bar should render a string.")
        public void setStringPainted​(boolean b)
        Sets the value of the 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 string
        See Also:
        isStringPainted(), setString(java.lang.String)
      • getString

        public String getString​()
        Returns a String representation of the current progress. By default, this returns a simple percentage String based on the value returned from getPercentComplete. An example would be the "42%". You can change this by calling setString.
        the value of the progress string, or a simple percentage string if the progress string is null
        See Also:
      • setString

                      description="Specifies the progress string to paint")
        public void setString​(String s)
        Sets the value of the progress string. By default, this string is 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
        See Also:
        getString(), setStringPainted(boolean), isStringPainted()
      • getPercentComplete

        public double getPercentComplete​()
        Returns the percent complete for the progress bar. Note that this number is between 0.0 and 1.0.
        the percent complete for this progress bar
      • isBorderPainted

        public boolean isBorderPainted​()
        Returns the borderPainted property.
        the value of the borderPainted property
        See Also:
      • setBorderPainted

                      description="Whether the progress bar should paint its border.")
        public void setBorderPainted​(boolean b)
        Sets the 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
        See Also:
      • getUI

        public ProgressBarUI getUI​()
        Returns the look-and-feel object that renders this component.
        getUI in class JComponent
        the ProgressBarUI object that renders this component
      • createChangeListener

        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. The default ChangeListener simply calls the fireStateChanged method to forward ChangeEvents to the ChangeListeners that have been added directly to the progress bar.
        the instance of a custom ChangeListener implementation.
        See Also:
        changeListener, fireStateChanged(), ChangeListener, BoundedRangeModel
      • addChangeListener

        public void addChangeListener​(ChangeListener l)
        Adds the specified ChangeListener to the progress bar.
        l - the ChangeListener to add
      • removeChangeListener

        public void removeChangeListener​(ChangeListener l)
        Removes a ChangeListener from the progress bar.
        l - the ChangeListener to remove
      • getChangeListeners

        public ChangeListener[] getChangeListeners​()
        Returns an array of all the ChangeListeners added to this progress bar with addChangeListener.
        all of the ChangeListeners added or an empty array if no listeners have been added
      • fireStateChanged

        protected void fireStateChanged​()
        Send a ChangeEvent, whose source is this JProgressBar, to all ChangeListeners that have registered interest in ChangeEvents. This method is called each time a ChangeEvent is received from the model.

        The event instance is created if necessary, and stored in changeEvent.

        See Also:
        addChangeListener(javax.swing.event.ChangeListener), EventListenerList
      • setModel

                      description="The data model used by the JProgressBar.")
        public void setModel​(BoundedRangeModel newModel)
        Sets the data model used by the JProgressBar. Note that the BoundedRangeModel's extent is not used, and is set to 0.
        newModel - the BoundedRangeModel to use
      • getValue

        public int getValue​()
        Returns the progress bar's current value from the BoundedRangeModel. The value is always between the minimum and maximum values, inclusive.
        the current value of the progress bar
        See Also:
        setValue(int), BoundedRangeModel.getValue()
      • setIndeterminate

        public void setIndeterminate​(boolean newValue)
        Sets the 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.
        See Also:
        isIndeterminate(), BasicProgressBarUI
      • isIndeterminate

                      description="Is the progress bar indeterminate (true) or normal (false)?")
        public boolean isIndeterminate​()
        Returns the value of the indeterminate property.
        the value of the indeterminate property
        See Also:
      • paramString

        protected String paramString​()
        Returns a string representation of this 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
        a string representation of this JProgressBar
      • getAccessibleContext

                      description="The AccessibleContext associated with this ProgressBar.")
        public AccessibleContext getAccessibleContext​()
        Gets the AccessibleContext associated with this JProgressBar. For progress bars, the AccessibleContext takes the form of an AccessibleJProgressBar. A new AccessibleJProgressBar instance is created if necessary.
        Specified by:
        getAccessibleContext in interface Accessible
        getAccessibleContext in class Component
        an AccessibleJProgressBar that serves as the AccessibleContext of this JProgressBar