|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Object![]()
![]()
![]()
java.awt.Component
![]()
![]()
![]()
java.awt.Container
![]()
![]()
![]()
javax.swing.JComponent
![]()
![]()
![]()
javax.swing.JSpinner
, MenuContainer
, Serializable
, Accessible

public class JSpinner


A single line input field that lets the user select a number or an object value from an ordered sequence. Spinners typically provide a pair of tiny arrow buttons for stepping through the elements of the sequence. The keyboard up/down arrow keys also cycle through the elements. The user may also be allowed to type a (legal) value directly into the spinner. Although combo boxes provide similar functionality, spinners are sometimes preferred because they don't require a drop down list that can obscure important data.
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();
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 XMLEncoder
.
SpinnerModel
,
AbstractSpinnerModel
,
SpinnerListModel
,
SpinnerNumberModel
,
SpinnerDateModel
,
JFormattedTextField
,
Serialized Form| Nested Class Summary | |
|---|---|
protected class |
JSpinner.AccessibleJSpinner
AccessibleJSpinner implements accessibility
support for the JSpinner class. |
static class |
JSpinner.DateEditor
An editor for a JSpinner whose model is a
SpinnerDateModel. |
static class |
JSpinner.DefaultEditor
A simple base class for more specialized editors that displays a read-only view of the model's current value with a JFormattedTextField |
static class |
JSpinner.ListEditor
An editor for a JSpinner whose model is a
SpinnerListModel. |
static class |
JSpinner.NumberEditor
An editor for a JSpinner whose model is a
SpinnerNumberModel. |
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 |
| Field Summary |
|---|
Fields inherited from class javax.swing.JComponent ![]() |
|---|
accessibleContext |
Fields inherited from class java.awt.Component ![]() |
|---|
BOTTOM_ALIGNMENT |
Fields inherited from interface java.awt.image.ImageObserver ![]() |
|---|
ABORT |
| Constructor Summary | |
|---|---|
JSpinner
Constructs a spinner with an Integer SpinnerNumberModel
with initial value 0 and no minimum or maximum limits. |
|
JSpinner
Constructs a complete spinner with pair of next/previous buttons and an editor for the SpinnerModel. |
|
| Method Summary | |
|---|---|
void |
addChangeListener
Adds a listener to the list that is notified each time a change to the model occurs. |
void |
commitEdit
Commits the currently edited value to the SpinnerModel. |
protected JComponent |
createEditor
This method is called by the constructors to create the JComponent
that displays the current value of the sequence. |
protected void |
fireStateChanged
Sends a ChangeEvent, whose source is this
JSpinner, to each ChangeListener. |
AccessibleContext |
getAccessibleContext
Gets the AccessibleContext |
ChangeListener |
getChangeListeners
Returns an array of all the ChangeListeners added
to this JSpinner with addChangeListener(). |
JComponent |
getEditor
Returns the component that displays and potentially changes the model's value. |
SpinnerModel |
getModel
Returns the SpinnerModel that defines
this spinners sequence of values. |
Object |
getNextValue
Returns the object in the sequence that comes after the object returned by getValue(). |
Object |
getPreviousValue
Returns the object in the sequence that comes before the object returned by getValue(). |
SpinnerUI |
getUI
Returns the look and feel (L&F) object that renders this component. |
String |
getUIClassID
Returns the suffix used to construct the name of the look and feel (L&F) class used to render this component. |
Object |
getValue
Returns the current value of the model, typically this value is displayed by the editor. |
void |
removeChangeListener
Removes a ChangeListener from this spinner. |
void |
setEditor
Changes the JComponent that displays the current value
of the SpinnerModel. |
void |
setModel
Changes the model that represents the value of this spinner. |
void |
setUI
Sets the look and feel (L&F) object that renders this component. |
void |
setValue
Changes current value of the model, typically this value is displayed by the editor. |
void |
updateUI
Resets the UI property with the value from the current look and feel. |