| public class javax.swing JSpinner
|
Java SE 6 |
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 JavaBeansTM
has been added to the java.beans package.
Please see java.beans.XMLEncoder.
| beaninfo | attribute: isContainer false description: A single line input field that lets the user select a number or an object value from an ordered set. |
| version | 1.52 08/08/06 |
| since | 1.4 |
| See also | javax.swing.SpinnerModel, javax.swing.AbstractSpinnerModel, javax.swing.SpinnerListModel, javax.swing.SpinnerNumberModel, javax.swing.SpinnerDateModel, javax.swing.JFormattedTextField |
| Constructors | |
|---|---|
| public | JSpinner(SpinnerModel model) Constructs a complete spinner with pair of next/previous buttons and an editor for the SpinnerModel.
|
| public | JSpinner() Constructs a spinner with an Integer SpinnerNumberModel
with initial value 0 and no minimum or maximum limits.
|
| Methods | |||||||
|---|---|---|---|---|---|---|---|
| public void | addChangeListener(ChangeListener listener) Adds a listener to the list that is notified each time a change to the model occurs. The source of ChangeEvents
delivered to ChangeListeners will be this
JSpinner. Note also that replacing the model
will not affect listeners added directly to JSpinner.
Applications can add listeners to the model directly. In that
case is that the source of the event would be the
SpinnerModel.
| ||||||
| public void | commitEdit() throws ParseException Commits the currently edited value to the SpinnerModel.
If the editor is an instance of
| ||||||
| protected JComponent | createEditor(SpinnerModel model) This method is called by the constructors to create the JComponent
that displays the current value of the sequence. The editor may
also allow the user to enter an element of the sequence directly.
An editor must listen for ChangeEvents on the
model and keep the value it displays
in sync with the value of the model.
Subclasses may override this method to add support for new
| ||||||
| protected void | fireStateChanged() Sends a ChangeEvent, whose source is this
JSpinner, to each ChangeListener.
When a ChangeListener has been added
to the spinner, this method method is called each time
a ChangeEvent is received from the model.
| ||||||
| public void | removeChangeListener(ChangeListener listener) Removes a ChangeListener from this spinner.
| ||||||
| public void | updateUI() Resets the UI property with the value from the current look and feel.
| ||||||
| Properties | |||||||||
|---|---|---|---|---|---|---|---|---|---|
| public AccessibleContext | getAccessibleContext() Gets the AccessibleContext for the JSpinner
| ||||||||
| public ChangeListener[] | getChangeListeners() Returns an array of all the ChangeListeners added
to this JSpinner with addChangeListener().
| ||||||||
| public void | setEditor(JComponent editor) Changes the JComponent that displays the current value
of the SpinnerModel. It is the responsibility of this
method to disconnect the old editor from the model and to
connect the new editor. This may mean removing the
old editors ChangeListener from the model or the
spinner itself and adding one for the new editor.
| ||||||||
| public JComponent | getEditor() Returns the component that displays and potentially changes the model's value.
| ||||||||
| public void | setModel(SpinnerModel model) Changes the model that represents the value of this spinner. If the editor property has not been explicitly set, the editor property is (implicitly) set after the "model"
PropertyChangeEvent has been fired. The editor
property is set to the value returned by createEditor,
as in:
setEditor(createEditor(model));
| ||||||||
| public SpinnerModel | getModel() Returns the SpinnerModel that defines
this spinners sequence of values.
| ||||||||
| public Object | getNextValue() Returns the object in the sequence that comes after the object returned by getValue(). If the end of the sequence has been reached
then return null.
Calling this method does not effect value.
This method simply delegates to the getModel().getNextValue()
| ||||||||
| public Object | getPreviousValue() Returns the object in the sequence that comes before the object returned by getValue().
If the end of the sequence has been reached then
return null. Calling this method does
not effect value.
This method simply delegates to the getModel().getPreviousValue()
| ||||||||
| public void | setUI(SpinnerUI ui) Sets the look and feel (L&F) object that renders this component.
| ||||||||
| public SpinnerUI | getUI() Returns the look and feel (L&F) object that renders this component.
| ||||||||
| public String | getUIClassID() Returns the suffix used to construct the name of the look and feel (L&F) class used to render this component.
| ||||||||
| public void | setValue(Object value) Changes current value of the model, typically this value is displayed by the editor.
If the SpinnerModel implementation
doesn't support the specified value then an
IllegalArgumentException is thrown.
This method simply delegates to the getModel().setValue(value)
| ||||||||
| public Object | getValue() Returns the current value of the model, typically this value is displayed by the editor. 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, refer to the class level javadoc for examples
of how to deal with this.
This method simply delegates to the getModel().getValue()
| ||||||||
| About DocWeb · Bundles · Export · Export All | Top 10 · Statistics · Login |
| About Sun · Contact · Privacy · Terms of Use · Trademarks | Java SE 6 · Copyright © 1994-2013 Sun Microsystems, Inc.All rights reserved. Use is subject to license terms |
![]() |
![]() |
|