Class Spinner<T>

Type Parameters:
T - The type of all values that can be iterated through in the Spinner. Common types include Integer and String.
All Implemented Interfaces:
Styleable, EventTarget, Skinnable

public class Spinner<T> extends Control
A single line text 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, and also because they allow for features such as wrapping and simpler specification of 'infinite' data models (the SpinnerValueFactory, rather than using a ObservableList data model like many other JavaFX UI controls.

A Spinner's sequence value is defined by its SpinnerValueFactory. The value factory can be specified as a constructor argument and changed with the value factory property. SpinnerValueFactory classes for some common types are provided with JavaFX, including:

A Spinner has a TextField child component that is responsible for displaying and potentially changing the current value of the Spinner, which is called the editor. By default the Spinner is non-editable, but input can be accepted if the editable property is set to true. The Spinner editor stays in sync with the value factory by listening for changes to the value property of the value factory. If the user has changed the value displayed in the editor it is possible for the Spinner value to differ from that of the editor. To make sure the model has the same value as the editor, the user must commit the edit using the Enter key.

Example:

Spinner spinner = new Spinner(0, 10, 5);
Image of the Spinner control
Since:
JavaFX 8u40
See Also:
  • Property Details

  • Field Details

    • STYLE_CLASS_ARROWS_ON_RIGHT_HORIZONTAL

      public static final String STYLE_CLASS_ARROWS_ON_RIGHT_HORIZONTAL
      The arrows are placed on the right of the Spinner, pointing horizontally (i.e. left and right).
      See Also:
    • STYLE_CLASS_ARROWS_ON_LEFT_VERTICAL

      public static final String STYLE_CLASS_ARROWS_ON_LEFT_VERTICAL
      The arrows are placed on the left of the Spinner, pointing vertically (i.e. up and down).
      See Also:
    • STYLE_CLASS_ARROWS_ON_LEFT_HORIZONTAL

      public static final String STYLE_CLASS_ARROWS_ON_LEFT_HORIZONTAL
      The arrows are placed on the left of the Spinner, pointing horizontally (i.e. left and right).
      See Also:
    • STYLE_CLASS_SPLIT_ARROWS_VERTICAL

      public static final String STYLE_CLASS_SPLIT_ARROWS_VERTICAL
      The arrows are placed above and beneath the spinner, stretching to take the entire width.
      See Also:
    • STYLE_CLASS_SPLIT_ARROWS_HORIZONTAL

      public static final String STYLE_CLASS_SPLIT_ARROWS_HORIZONTAL
      The decrement arrow is placed on the left of the Spinner, and the increment on the right.
      See Also:
  • Constructor Details

    • Spinner

      public Spinner()
      Constructs a default Spinner instance, with the default 'spinner' style class and a non-editable editor.
    • Spinner

      public Spinner(int min, int max, int initialValue)
      Creates a Spinner instance with the value factory set to be an instance of SpinnerValueFactory.IntegerSpinnerValueFactory. Note that if this constructor is called, the only valid generic type for the Spinner instance is Integer, i.e. Spinner<Integer>.
      Parameters:
      min - The minimum allowed integer value for the Spinner.
      max - The maximum allowed integer value for the Spinner.
      initialValue - The value of the Spinner when first instantiated, must be within the bounds of the min and max arguments, or else the min value will be used.
    • Spinner

      public Spinner(int min, int max, int initialValue, int amountToStepBy)
      Creates a Spinner instance with the value factory set to be an instance of SpinnerValueFactory.IntegerSpinnerValueFactory. Note that if this constructor is called, the only valid generic type for the Spinner instance is Integer, i.e. Spinner<Integer>.
      Parameters:
      min - The minimum allowed integer value for the Spinner.
      max - The maximum allowed integer value for the Spinner.
      initialValue - The value of the Spinner when first instantiated, must be within the bounds of the min and max arguments, or else the min value will be used.
      amountToStepBy - The amount to increment or decrement by, per step.
    • Spinner

      public Spinner(double min, double max, double initialValue)
      Creates a Spinner instance with the value factory set to be an instance of SpinnerValueFactory.DoubleSpinnerValueFactory. Note that if this constructor is called, the only valid generic type for the Spinner instance is Double, i.e. Spinner<Double>.
      Parameters:
      min - The minimum allowed double value for the Spinner.
      max - The maximum allowed double value for the Spinner.
      initialValue - The value of the Spinner when first instantiated, must be within the bounds of the min and max arguments, or else the min value will be used.
    • Spinner

      public Spinner(double min, double max, double initialValue, double amountToStepBy)
      Creates a Spinner instance with the value factory set to be an instance of SpinnerValueFactory.DoubleSpinnerValueFactory. Note that if this constructor is called, the only valid generic type for the Spinner instance is Double, i.e. Spinner<Double>.
      Parameters:
      min - The minimum allowed double value for the Spinner.
      max - The maximum allowed double value for the Spinner.
      initialValue - The value of the Spinner when first instantiated, must be within the bounds of the min and max arguments, or else the min value will be used.
      amountToStepBy - The amount to increment or decrement by, per step.
    • Spinner

      public Spinner(ObservableList<T> items)
      Creates a Spinner instance with the value factory set to be an instance of SpinnerValueFactory.ListSpinnerValueFactory. The Spinner value property will be set to the first element of the list, if an element exists, or null otherwise.
      Parameters:
      items - A list of items that will be stepped through in the Spinner.
    • Spinner

      public Spinner(SpinnerValueFactory<T> valueFactory)
      Creates a Spinner instance with the given value factory set.
      Parameters:
      valueFactory - The value factory to use.
  • Method Details

    • increment

      public void increment()
      Attempts to increment the value factory by one step, by calling the SpinnerValueFactory.increment(int) method with an argument of one. If the value factory is null, an IllegalStateException is thrown.
      Throws:
      IllegalStateException - if the value factory returned by calling getValueFactory() is null.
    • increment

      public void increment(int steps)
      Attempts to increment the value factory by the given number of steps, by calling the SpinnerValueFactory.increment(int) method and forwarding the steps argument to it. If the value factory is null, an IllegalStateException is thrown.
      Parameters:
      steps - The number of increments that should be performed on the value.
      Throws:
      IllegalStateException - if the value factory returned by calling getValueFactory() is null.
    • decrement

      public void decrement()
      Attempts to decrement the value factory by one step, by calling the SpinnerValueFactory.decrement(int) method with an argument of one. If the value factory is null, an IllegalStateException is thrown.
      Throws:
      IllegalStateException - if the value factory returned by calling getValueFactory() is null.
    • decrement

      public void decrement(int steps)
      Attempts to decrement the value factory by the given number of steps, by calling the SpinnerValueFactory.decrement(int) method and forwarding the steps argument to it. If the value factory is null, an IllegalStateException is thrown.
      Parameters:
      steps - The number of decrements that should be performed on the value.
      Throws:
      IllegalStateException - if the value factory returned by calling getValueFactory() is null.
    • commitValue

      public final void commitValue()
      If the Spinner is editable, calling this method will attempt to commit the current text and convert it to a value.
      Since:
      9
    • cancelEdit

      public final void cancelEdit()
      If the Spinner is editable, calling this method will attempt to replace the editor text with the last committed value.
      Since:
      9
    • getValue

      public final T getValue()
      Gets the value of the value property.
      Property description:
      The value property on Spinner is a read-only property, as it is bound to the SpinnerValueFactory value property. Should the value factory change, this value property will be unbound from the old value factory and bound to the new one.

      If developers wish to modify the value property, they may do so with code in the following form:

       
       Object newValue = ...;
       spinner.getValueFactory().setValue(newValue);
       
      Returns:
      the value of the value property
      See Also:
    • valueProperty

      public final ReadOnlyObjectProperty<T> valueProperty()
      The value property on Spinner is a read-only property, as it is bound to the SpinnerValueFactory value property. Should the value factory change, this value property will be unbound from the old value factory and bound to the new one.

      If developers wish to modify the value property, they may do so with code in the following form:

       
       Object newValue = ...;
       spinner.getValueFactory().setValue(newValue);
       
      Returns:
      the value property
      See Also:
    • setValueFactory

      public final void setValueFactory(SpinnerValueFactory<T> value)
      Sets the value of the valueFactory property.
      Property description:
      The value factory is the model behind the JavaFX Spinner control - without a value factory installed a Spinner is unusable. It is the role of the value factory to handle almost all aspects of the Spinner, including:
      Parameters:
      value - the value for the valueFactory property
      See Also:
    • getValueFactory

      public final SpinnerValueFactory<T> getValueFactory()
      Gets the value of the valueFactory property.
      Property description:
      The value factory is the model behind the JavaFX Spinner control - without a value factory installed a Spinner is unusable. It is the role of the value factory to handle almost all aspects of the Spinner, including:
      Returns:
      the value of the valueFactory property
      See Also:
    • valueFactoryProperty

      public final ObjectProperty<SpinnerValueFactory<T>> valueFactoryProperty()
      The value factory is the model behind the JavaFX Spinner control - without a value factory installed a Spinner is unusable. It is the role of the value factory to handle almost all aspects of the Spinner, including:
      Returns:
      the valueFactory property
      See Also:
    • setEditable

      public final void setEditable(boolean value)
      Sets the value of the editable property.
      Property description:
      The editable property is used to specify whether user input is able to be typed into the Spinner editor. If editable is true, user input will be received once the user types and presses the Enter key. At this point the input is passed to the SpinnerValueFactory converter StringConverter.fromString(String) method. The returned value from this call (of type T) is then sent to the SpinnerValueFactory.setValue(Object) method. If the value is valid, it will remain as the value. If it is invalid, the value factory will need to react accordingly and back out this change.
      Default value:
      false
      Parameters:
      value - the value for the editable property
      See Also:
    • isEditable

      public final boolean isEditable()
      Gets the value of the editable property.
      Property description:
      The editable property is used to specify whether user input is able to be typed into the Spinner editor. If editable is true, user input will be received once the user types and presses the Enter key. At this point the input is passed to the SpinnerValueFactory converter StringConverter.fromString(String) method. The returned value from this call (of type T) is then sent to the SpinnerValueFactory.setValue(Object) method. If the value is valid, it will remain as the value. If it is invalid, the value factory will need to react accordingly and back out this change.
      Default value:
      false
      Returns:
      the value of the editable property
      See Also:
    • editableProperty

      public final BooleanProperty editableProperty()
      The editable property is used to specify whether user input is able to be typed into the Spinner editor. If editable is true, user input will be received once the user types and presses the Enter key. At this point the input is passed to the SpinnerValueFactory converter StringConverter.fromString(String) method. The returned value from this call (of type T) is then sent to the SpinnerValueFactory.setValue(Object) method. If the value is valid, it will remain as the value. If it is invalid, the value factory will need to react accordingly and back out this change.
      Default value:
      false
      Returns:
      the editable property
      See Also:
    • editorProperty

      public final ReadOnlyObjectProperty<TextField> editorProperty()
      The editor used by the Spinner control.
      Returns:
      the editor property
      See Also:
    • getEditor

      public final TextField getEditor()
      Gets the value of the editor property.
      Property description:
      The editor used by the Spinner control.
      Returns:
      the value of the editor property
      See Also:
    • promptTextProperty

      public final StringProperty promptTextProperty()
      The prompt text to display in the Spinner, or null if no prompt text is displayed.
      Default value:
      Empty string
      Returns:
      the prompt text property
      Since:
      9
      See Also:
    • getPromptText

      public final String getPromptText()
      Gets the value of the promptText property.
      Property description:
      The prompt text to display in the Spinner, or null if no prompt text is displayed.
      Default value:
      Empty string
      Returns:
      the value of the promptText property
      Since:
      9
      See Also:
    • setPromptText

      public final void setPromptText(String value)
      Sets the value of the promptText property.
      Property description:
      The prompt text to display in the Spinner, or null if no prompt text is displayed.
      Default value:
      Empty string
      Parameters:
      value - the value for the promptText property
      Since:
      9
      See Also:
    • initialDelayProperty

      public final ObjectProperty<Duration> initialDelayProperty()
      The duration that the mouse has to be pressed on an arrow button before the next value steps. Successive step duration is set using repeat delay.
      Default value:
      300ms
      Returns:
      initial delay property
      Since:
      11
      See Also:
    • setInitialDelay

      public final void setInitialDelay(Duration value)
      Sets the value of the initialDelay property.
      Property description:
      The duration that the mouse has to be pressed on an arrow button before the next value steps. Successive step duration is set using repeat delay.
      Default value:
      300ms
      Parameters:
      value - the value for the initialDelay property
      Since:
      11
      See Also:
    • getInitialDelay

      public final Duration getInitialDelay()
      Gets the value of the initialDelay property.
      Property description:
      The duration that the mouse has to be pressed on an arrow button before the next value steps. Successive step duration is set using repeat delay.
      Default value:
      300ms
      Returns:
      the value of the initialDelay property
      Since:
      11
      See Also:
    • repeatDelayProperty

      public final ObjectProperty<Duration> repeatDelayProperty()
      The duration that the mouse has to be pressed for each successive step after the first value steps. Initial step duration is set using initial delay.
      Default value:
      60ms
      Returns:
      repeat delay property
      Since:
      11
      See Also:
    • setRepeatDelay

      public final void setRepeatDelay(Duration value)
      Sets the value of the repeatDelay property.
      Property description:
      The duration that the mouse has to be pressed for each successive step after the first value steps. Initial step duration is set using initial delay.
      Default value:
      60ms
      Parameters:
      value - the value for the repeatDelay property
      Since:
      11
      See Also:
    • getRepeatDelay

      public final Duration getRepeatDelay()
      Gets the value of the repeatDelay property.
      Property description:
      The duration that the mouse has to be pressed for each successive step after the first value steps. Initial step duration is set using initial delay.
      Default value:
      60ms
      Returns:
      the value of the repeatDelay property
      Since:
      11
      See Also:
    • getClassCssMetaData

      public static List<CssMetaData<? extends Styleable,?>> getClassCssMetaData()
      Gets the CssMetaData associated with this class, which may include the CssMetaData of its superclasses.
      Returns:
      the CssMetaData
      Since:
      11
    • getControlCssMetaData

      public List<CssMetaData<? extends Styleable,?>> getControlCssMetaData()
      Description copied from class: Control
      Gets the unmodifiable list of the control's CSS-styleable properties.
      Overrides:
      getControlCssMetaData in class Control
      Returns:
      the unmodifiable list of the control's CSS-styleable properties