Fields Summary |
---|
private static final String | uiClassID |
protected int | orientationWhether the progress bar is horizontal or vertical.
The default is HORIZONTAL . |
protected boolean | paintBorderWhether to display a border around the progress bar.
The default is true . |
protected BoundedRangeModel | modelThe object that holds the data for the progress bar. |
protected String | progressStringAn 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, {@code paintString} must be {@code true}. |
protected boolean | paintStringWhether 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. |
private static final int | defaultMinimumThe default minimum for a progress bar is 0. |
private static final int | defaultMaximumThe default maximum for a progress bar is 100. |
private static final int | defaultOrientationThe default orientation for a progress bar is HORIZONTAL . |
protected transient ChangeEvent | changeEventOnly 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. |
protected ChangeListener | changeListenerListens for change events sent by the progress bar's model,
redispatching them
to change-event listeners registered upon
this progress bar. |
private transient Format | formatFormat used when displaying percent complete. |
private boolean | indeterminateWhether the progress bar is indeterminate (true ) or
normal (false ); the default is false . |
Constructors 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.
this(defaultOrientation);
|
public JProgressBar(int orient)Creates a progress bar with the specified orientation,
which can be
either {@code SwingConstants.VERTICAL} or
{@code 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.
this(orient, defaultMinimum, defaultMaximum);
|
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 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 {@code BoundedRangeModel} documentation for details.
this(defaultOrientation, min, max);
|
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 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 {@code BoundedRangeModel} documentation for details.
// Creating the model this way is a bit simplistic, but
// I believe that it is the the most common usage of this
// component - it's what people will expect.
setModel(new DefaultBoundedRangeModel(min, 0, min, max));
updateUI();
setOrientation(orient); // documented with set/getOrientation()
setBorderPainted(true); // documented with is/setBorderPainted()
setStringPainted(false); // see setStringPainted
setString(null); // see getString
setIndeterminate(false); // see setIndeterminate
|
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.
setModel(newModel);
updateUI();
setOrientation(defaultOrientation); // see setOrientation()
setBorderPainted(true); // see setBorderPainted()
setStringPainted(false); // see setStringPainted
setString(null); // see getString
setIndeterminate(false); // see setIndeterminate
|
Methods Summary |
---|
public void | addChangeListener(javax.swing.event.ChangeListener l)Adds the specified ChangeListener to the progress bar.
listenerList.add(ChangeListener.class, l);
|
protected javax.swing.event.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 {@code ChangeListener} simply calls the
{@code fireStateChanged} method to forward {@code ChangeEvent}s
to the {@code ChangeListener}s that have been added directly to the
progress bar.
return new ModelListener();
|
protected void | fireStateChanged()Send a {@code ChangeEvent}, whose source is this {@code JProgressBar}, to
all {@code ChangeListener}s that have registered interest in
{@code ChangeEvent}s.
This method is called each time a {@code ChangeEvent} is received from
the model.
The event instance is created if necessary, and stored in
{@code changeEvent}.
// Guaranteed to return a non-null array
Object[] listeners = listenerList.getListenerList();
// Process the listeners last to first, notifying
// those that are interested in this event
for (int i = listeners.length-2; i>=0; i-=2) {
if (listeners[i]==ChangeListener.class) {
// Lazily create the event:
if (changeEvent == null)
changeEvent = new ChangeEvent(this);
((ChangeListener)listeners[i+1]).stateChanged(changeEvent);
}
}
|
public javax.accessibility.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.
if (accessibleContext == null) {
accessibleContext = new AccessibleJProgressBar();
}
return accessibleContext;
|
public javax.swing.event.ChangeListener[] | getChangeListeners()Returns an array of all the ChangeListener s added
to this progress bar with addChangeListener .
return (ChangeListener[])listenerList.getListeners(
ChangeListener.class);
|
public int | getMaximum()Returns the progress bar's {@code maximum} value
from the BoundedRangeModel . return getModel().getMaximum();
|
public int | getMinimum()Returns the progress bar's {@code minimum} value
from the BoundedRangeModel . return getModel().getMinimum();
|
public javax.swing.BoundedRangeModel | getModel()Returns the data model used by this progress bar.
return model;
|
public int | getOrientation()Returns {@code SwingConstants.VERTICAL} or
{@code SwingConstants.HORIZONTAL}, depending on the orientation
of the progress bar. The default orientation is
{@code SwingConstants.HORIZONTAL}.
return orientation;
|
public double | getPercentComplete()Returns the percent complete for the progress bar.
Note that this number is between 0.0 and 1.0.
long span = model.getMaximum() - model.getMinimum();
double currentValue = model.getValue();
double pc = (currentValue - model.getMinimum()) / span;
return pc;
|
public java.lang.String | getString()Returns a {@code String} representation of the current progress.
By default, this returns a simple percentage {@code String} based on
the value returned from {@code getPercentComplete}. An example
would be the "42%". You can change this by calling {@code setString}.
if (progressString != null) {
return progressString;
} else {
if (format == null) {
format = NumberFormat.getPercentInstance();
}
return format.format(new Double(getPercentComplete()));
}
|
public javax.swing.plaf.ProgressBarUI | getUI()Returns the look-and-feel object that renders this component.
return (ProgressBarUI)ui;
|
public java.lang.String | getUIClassID()Returns the name of the look-and-feel class that renders this component.
return uiClassID;
|
public int | getValue()Returns the progress bar's current {@code value}
from the BoundedRangeModel .
The value is always between the
minimum and maximum values, inclusive. return getModel().getValue();
|
public boolean | isBorderPainted()Returns the borderPainted property.
return paintBorder;
|
public boolean | isIndeterminate()Returns the value of the indeterminate property.
return indeterminate;
|
public boolean | isStringPainted()Returns the value of the stringPainted property.
return paintString;
|
protected void | paintBorder(java.awt.Graphics g)Paints the progress bar's border if the borderPainted
property is true .
if (isBorderPainted()) {
super.paintBorder(g);
}
|
protected java.lang.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 .
String orientationString = (orientation == HORIZONTAL ?
"HORIZONTAL" : "VERTICAL");
String paintBorderString = (paintBorder ?
"true" : "false");
String progressStringString = (progressString != null ?
progressString : "");
String paintStringString = (paintString ?
"true" : "false");
String indeterminateString = (indeterminate ?
"true" : "false");
return super.paramString() +
",orientation=" + orientationString +
",paintBorder=" + paintBorderString +
",paintString=" + paintStringString +
",progressString=" + progressStringString +
",indeterminateString=" + indeterminateString;
|
public void | removeChangeListener(javax.swing.event.ChangeListener l)Removes a ChangeListener from the progress bar.
listenerList.remove(ChangeListener.class, l);
|
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.
boolean oldValue = paintBorder;
paintBorder = b;
firePropertyChange("borderPainted", oldValue, paintBorder);
if (paintBorder != oldValue) {
repaint();
}
|
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.
boolean oldValue = indeterminate;
indeterminate = newValue;
firePropertyChange("indeterminate", oldValue, indeterminate);
|
public void | setMaximum(int n)Sets the progress bar's maximum value
(stored in the progress bar's data model) to n .
The underlying BoundedRangeModel handles any mathematical
issues arising from assigning faulty values.
See the {@code BoundedRangeModel} documentation for details.
If the maximum value is different from the previous maximum,
all change listeners are notified. getModel().setMaximum(n);
|
public void | setMinimum(int n)Sets the progress bar's minimum value
(stored in the progress bar's data model) to n .
The data model (a BoundedRangeModel instance)
handles any mathematical
issues arising from assigning faulty values.
See the {@code BoundedRangeModel} documentation for details.
If the minimum value is different from the previous minimum,
all change listeners are notified. getModel().setMinimum(n);
|
public void | setModel(javax.swing.BoundedRangeModel newModel)Sets the data model used by the JProgressBar .
Note that the {@code BoundedRangeModel}'s {@code extent} is not used,
and is set to {@code 0}.
// PENDING(???) setting the same model to multiple bars is broken; listeners
BoundedRangeModel oldModel = getModel();
if (newModel != oldModel) {
if (oldModel != null) {
oldModel.removeChangeListener(changeListener);
changeListener = null;
}
model = newModel;
if (newModel != null) {
changeListener = createChangeListener();
newModel.addChangeListener(changeListener);
}
if (accessibleContext != null) {
accessibleContext.firePropertyChange(
AccessibleContext.ACCESSIBLE_VALUE_PROPERTY,
(oldModel== null
? null : new Integer(oldModel.getValue())),
(newModel== null
? null : new Integer(newModel.getValue())));
}
if (model != null) {
model.setExtent(0);
}
repaint();
}
|
public void | setOrientation(int newOrientation)Sets the progress bar's orientation to newOrientation ,
which must be {@code SwingConstants.VERTICAL} or
{@code SwingConstants.HORIZONTAL}. The default orientation
is {@code SwingConstants.HORIZONTAL}.
if (orientation != newOrientation) {
switch (newOrientation) {
case VERTICAL:
case HORIZONTAL:
int oldOrientation = orientation;
orientation = newOrientation;
firePropertyChange("orientation", oldOrientation, newOrientation);
if (accessibleContext != null) {
accessibleContext.firePropertyChange(
AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
((oldOrientation == VERTICAL)
? AccessibleState.VERTICAL
: AccessibleState.HORIZONTAL),
((orientation == VERTICAL)
? AccessibleState.VERTICAL
: AccessibleState.HORIZONTAL));
}
break;
default:
throw new IllegalArgumentException(newOrientation +
" is not a legal orientation");
}
revalidate();
}
|
public void | setString(java.lang.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 .
String oldValue = progressString;
progressString = s;
firePropertyChange("string", oldValue, progressString);
if (progressString == null || oldValue == null || !progressString.equals(oldValue)) {
repaint();
}
|
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.
//PENDING: specify that string not painted when in indeterminate mode?
// or just leave that to the L&F?
boolean oldValue = paintString;
paintString = b;
firePropertyChange("stringPainted", oldValue, paintString);
if (paintString != oldValue) {
revalidate();
repaint();
}
|
public void | setUI(javax.swing.plaf.ProgressBarUI ui)Sets the look-and-feel object that renders this component.
super.setUI(ui);
|
public void | setValue(int n)Sets the progress bar's current value to {@code n}. This method
forwards the new value to the model.
The data model (an instance of {@code BoundedRangeModel})
handles any mathematical
issues arising from assigning faulty values. See the
{@code BoundedRangeModel} documentation for details.
If the new value is different from the previous value,
all change listeners are notified.
BoundedRangeModel brm = getModel();
int oldValue = brm.getValue();
brm.setValue(n);
if (accessibleContext != null) {
accessibleContext.firePropertyChange(
AccessibleContext.ACCESSIBLE_VALUE_PROPERTY,
new Integer(oldValue),
new Integer(brm.getValue()));
}
|
public void | updateUI()Resets the UI property to a value from the current look and feel.
setUI((ProgressBarUI)UIManager.getUI(this));
|
private void | writeObject(java.io.ObjectOutputStream s)See readObject() and writeObject() in JComponent for more
information about serialization in Swing.
s.defaultWriteObject();
if (getUIClassID().equals(uiClassID)) {
byte count = JComponent.getWriteObjCounter(this);
JComponent.setWriteObjCounter(this, --count);
if (count == 0 && ui != null) {
ui.installUI(this);
}
}
|