Class JSpinner

java.lang.Object
java.awt.Component
java.awt.Container
javax.swing.JComponent
javax.swing.JSpinner
All Implemented Interfaces:
ImageObserver,MenuContainer,Serializable,Accessible

@JavaBean(defaultProperty="UI",description="A single line input field that lets the user select a number or an object value from an ordered set.")public classJSpinnerextendsJComponentimplementsAccessible
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.

AJSpinner's sequence value is defined by itsSpinnerModel. Themodel can be specified as a constructor argument and changed with themodel property.SpinnerModel classes for some common types are provided:SpinnerListModel,SpinnerNumberModel, andSpinnerDateModel.

AJSpinner has a single child component that's responsible for displaying and potentially changing the current element orvalue of the model, which is called theeditor. The editor is created by theJSpinner's constructor and can be changed with theeditor property. TheJSpinner's editor stays in sync with the model by listening forChangeEvents. If the user has changed the value displayed by theeditor it is possible for themodel's value to differ from that of theeditor. To make sure themodel has the same value as the editor use thecommitEdit 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 seeHow to Use Spinners, a section inThe Java Tutorial.

Warning: Swing is not thread safe. For more information seeSwing'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 thejava.beans package. Please seeXMLEncoder.

Since:
1.4
See Also:
  • Constructor Details

    • JSpinner

      public JSpinner(SpinnerModel model)
      Constructs a spinner for the given model. The spinner has a set of previous/next buttons, and an editor appropriate for the model.
      Parameters:
      model - a model for the new spinner
      Throws:
      NullPointerException - if the model isnull
    • JSpinner

      public JSpinner()
      Constructs a spinner with anInteger SpinnerNumberModel with initial value 0 and no minimum or maximum limits.
  • Method Details

    • getUI

      public SpinnerUI getUI()
      Returns the look and feel (L&F) object that renders this component.
      Overrides:
      getUI in class JComponent
      Returns:
      theSpinnerUI object that renders this component
    • setUI

      public void setUI(SpinnerUI ui)
      Sets the look and feel (L&F) object that renders this component.
      Parameters:
      ui - theSpinnerUI L&F object
      See Also:
    • getUIClassID

      @BeanProperty(bound=false)public String getUIClassID()
      Returns the suffix used to construct the name of the look and feel (L&F) class used to render this component.
      Overrides:
      getUIClassID in class JComponent
      Returns:
      the string "SpinnerUI"
      See Also:
    • updateUI

      public void updateUI()
      Resets the UI property with the value from the current look and feel.
      Overrides:
      updateUI in class JComponent
      See Also:
    • createEditor

      protected JComponent createEditor(SpinnerModel model)
      This method is called by the constructors to create theJComponent 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 forChangeEvents on themodel and keep the value it displays in sync with the value of the model.

      Subclasses may override this method to add support for newSpinnerModel classes. Alternatively one can just replace the editor created here with thesetEditor method. The default mapping from model type to editor is:

      • SpinnerNumberModel => JSpinner.NumberEditor
      • SpinnerDateModel => JSpinner.DateEditor
      • SpinnerListModel => JSpinner.ListEditor
      • all others =>JSpinner.DefaultEditor

      Parameters:
      model - the value of getModel
      Returns:
      a component that displays the current value of the sequence
      See Also:
    • setModel

      @BeanProperty(visualUpdate=true,description="Model that represents the value of this spinner.")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 bycreateEditor, as in:
       setEditor(createEditor(model));
      Parameters:
      model - the newSpinnerModel
      Throws:
      IllegalArgumentException - if model isnull
      See Also:
    • getModel

      public SpinnerModel getModel()
      Returns theSpinnerModel that defines this spinners sequence of values.
      Returns:
      the value of the model property
      See Also:
    • getValue

      public Object getValue()
      Returns the current value of the model, typically this value is displayed by theeditor. If the user has changed the value displayed by theeditor it is possible for themodel's value to differ from that of theeditor, refer to the class level javadoc for examples of how to deal with this.

      This method simply delegates to themodel. It is equivalent to:

       getModel().getValue()

      Returns:
      the current value of the model
      See Also:
    • setValue

      public void setValue(Object value)
      Changes current value of the model, typically this value is displayed by theeditor. If theSpinnerModel implementation doesn't support the specified value then anIllegalArgumentException is thrown.

      This method simply delegates to themodel. It is equivalent to:

       getModel().setValue(value)

      Parameters:
      value - new value for the spinner
      Throws:
      IllegalArgumentException - ifvalue isn't allowed
      See Also:
    • getNextValue

      @BeanProperty(bound=false)public Object getNextValue()
      Returns the object in the sequence that comes after the object returned bygetValue(). If the end of the sequence has been reached then returnnull. Calling this method does not effectvalue.

      This method simply delegates to themodel. It is equivalent to:

       getModel().getNextValue()

      Returns:
      the next legal value ornull if one doesn't exist
      See Also:
    • addChangeListener

      public void addChangeListener(ChangeListener listener)
      Adds a listener to the list that is notified each time a change to the model occurs. The source ofChangeEvents delivered toChangeListeners will be thisJSpinner. 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 theSpinnerModel.
      Parameters:
      listener - theChangeListener to add
      See Also:
    • removeChangeListener

      public void removeChangeListener(ChangeListener listener)
      Removes aChangeListener from this spinner.
      Parameters:
      listener - theChangeListener to remove
      See Also:
    • getChangeListeners

      @BeanProperty(bound=false)public ChangeListener[] getChangeListeners()
      Returns an array of all theChangeListeners added to this JSpinner with addChangeListener().
      Returns:
      all of theChangeListeners added or an empty array if no listeners have been added
      Since:
      1.4
    • fireStateChanged

      protected void fireStateChanged()
      Sends aChangeEvent, whose source is thisJSpinner, to eachChangeListener. When aChangeListener has been added to the spinner, this method is called each time aChangeEvent is received from the model.
      See Also:
    • getPreviousValue

      @BeanProperty(bound=false)public Object getPreviousValue()
      Returns the object in the sequence that comes before the object returned bygetValue(). If the end of the sequence has been reached then returnnull. Calling this method does not effectvalue.

      This method simply delegates to themodel. It is equivalent to:

       getModel().getPreviousValue()

      Returns:
      the previous legal value ornull if one doesn't exist
      See Also:
    • setEditor

      @BeanProperty(visualUpdate=true,description="JComponent that displays the current value of the model")public void setEditor(JComponent editor)
      Changes theJComponent that displays the current value of theSpinnerModel. It is the responsibility of this method todisconnect the old editor from the model and to connect the new editor. This may mean removing the old editorsChangeListener from the model or the spinner itself and adding one for the new editor.
      Parameters:
      editor - the new editor
      Throws:
      IllegalArgumentException - if editor isnull
      See Also:
    • getEditor

      public JComponent getEditor()
      Returns the component that displays and potentially changes the model's value.
      Returns:
      the component that displays and potentially changes the model's value
      See Also:
    • commitEdit

      public void commitEdit() throwsParseException
      Commits the currently edited value to theSpinnerModel.

      If the editor is an instance ofDefaultEditor, the call if forwarded to the editor, otherwise this does nothing.

      Throws:
      ParseException - if the currently edited value couldn't be committed.
    • getAccessibleContext

      @BeanProperty(bound=false)public AccessibleContext getAccessibleContext()
      Gets theAccessibleContext for theJSpinner
      Specified by:
      getAccessibleContext in interface Accessible
      Overrides:
      getAccessibleContext in class Component
      Returns:
      theAccessibleContext for theJSpinner
      Since:
      1.5