A JSpinner's sequence value is defined by its
SpinnerModel.
The model can be specified as a constructor argument and
changed with the model property. SpinnerModel
classes for some common types are provided: SpinnerListModel,
SpinnerNumberModel, and SpinnerDateModel.
A JSpinner has a single child component that's
responsible for displaying
and potentially changing the current element or value of
the model, which is called the editor. The editor is created
by the JSpinner's constructor and can be changed with the
editor property. The JSpinner's editor stays
in sync with the model by listening for ChangeEvents. If the
user has changed the value displayed by the editor it is
possible for the model's value to differ from that of
the editor. To make sure the model has the same
value as the editor use the commitEdit method, eg:
try {
spinner.commitEdit();
}
catch (ParseException pe) {
// Edited value is invalid, spinner.getValue() will return
// the last valid value, you could revert the spinner to show that:
JComponent editor = spinner.getEditor();
if (editor instanceof DefaultEditor) {
((DefaultEditor)editor).getTextField().setValue(spinner.getValue());
}
// reset the value to some known value:
spinner.setValue(fallbackValue);
// or treat the last valid value as the current, in which
// case you don't need to do anything.
}
return spinner.getValue();
For information and examples of using spinner see How to Use Spinners, 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 java..
SpinnerModel, AbstractSpinnerModel, SpinnerListModel, SpinnerNumberModel, SpinnerDateModel, JFormattedTextField
| Modifier and Type | Class and Description |
|---|---|
| protected class | JSpinner.
|
| public static class | JSpinner.
An editor for a |
| private static class | JSpinner.
This subclass of javax.swing.DateFormatter maps the minimum/maximum properties to the start/end properties of a SpinnerDateModel. |
| public static class | JSpinner.
A simple base class for more specialized editors
that displays a read-only view of the model's current
value with a |
| private static class | JSpinner.
An Action implementation that is always disabled. |
| public static class | JSpinner.
An editor for a |
| private class | JSpinner.
We pass |
| public static class | JSpinner.
An editor for a |
| private static class | JSpinner.
This subclass of javax.swing.NumberFormatter maps the minimum/maximum properties to a SpinnerNumberModel and initializes the valueClass of the NumberFormatter to match the type of the initial models value. |
| Modifier and Type | Field and Description |
|---|---|
| private transient ChangeEvent | |
| private static final Action | |
| private JComponent | |
| private boolean | |
| private SpinnerModel | |
| private ChangeListener | |
| private static final String |
| Access | Constructor and Description |
|---|---|
| public | |
| public | JSpinner()
Constructs a spinner with an |
| Modifier and Type | Method and Description |
|---|---|
| public void | addChangeListener(ChangeListener
the listener)ChangeListener to addAdds a listener to the list that is notified each time a change to the model occurs. |
| public void | |
| protected JComponent | Returns: a component that displays the current value of the sequencethe value of getModel model)This method is called by the constructors to create the
|
| protected void | |
| public AccessibleContext | Returns: theAccessibleContext for the JSpinnerOverrides java. Implements javax. Gets the |
| public ChangeListener[] | Returns: all of theChangeListeners added or an empty
array if no listeners have been addedReturns an array of all the |
| public JComponent | Returns: the component that displays and potentially changes the model's valueReturns the component that displays and potentially changes the model's value. |
| public SpinnerModel | Returns: the value of the model propertyReturns the |
| public Object | Returns: the next legal value ornull if one doesn't existReturns the object in the sequence that comes after the object returned
by |
| public Object | Returns: the previous legal value ornull
if one doesn't existReturns the object in the sequence that comes
before the object returned by |
| public SpinnerUI | Returns: theSpinnerUI object that renders this componentOverrides javax. Returns the look and feel (L&F) object that renders this component. |
| public String | Returns: the string "SpinnerUI"Overrides javax. Returns the suffix used to construct the name of the look and feel (L&F) class used to render this component. |
| public Object | Returns: the current value of the modelReturns the current value of the model, typically
this value is displayed by the |
| public void | removeChangeListener(ChangeListener
the listener)ChangeListener to removeRemoves a |
| public void | setEditor(JComponent
the new editor editor)Changes the |
| public void | setModel(SpinnerModel
the new model)SpinnerModelChanges the model that represents the value of this spinner. |
| public void | |
| public void | |
| public void | updateUI()
Overrides javax. Resets the UI property with the value from the current look and feel. |
| private void | writeObject(ObjectOutputStream s)
Hides javax. Before writing a |