Class Stepper

All Implemented Interfaces:
Animation, Editable, StyleListener, Iterable<Component>

public class Stepper extends Container

A numeric field with increment and decrement controls: NSStepper, WinUI's NumberBox with its spin buttons, GtkSpinButton.

Codename One had no equivalent. The nearest thing is Slider, which is a different control for a different job -- a slider is for a value whose exact number does not matter, and a stepper is for one where it does. An application that needed a bounded number on the desktop had to build the composite by hand, and every hand-built one got the same two things wrong: it let the field hold text that is not a number, and it let the buttons walk past the bounds.

Stepper copies = new Stepper(1, 1, 99);
copies.addActionListener(e -> print(copies.getValue()));

Three UIIDs: Stepper for the composite, StepperField for the text field and StepperButton for the two buttons. The buttons carry - and + by default; a theme or an application that wants arrows sets icons on #getDecrementButton() and #getIncrementButton().

The value is an int. A stepper over a fractional quantity is a real control on some platforms, and it is deliberately not this one: doing it properly means a format, a locale and a parse policy, and guessing those is worse than not offering them.

  • Constructor Details

    • Stepper

      public Stepper()
      A stepper over 0..100 starting at 0.
    • Stepper

      public Stepper(int value, int minValue, int maxValue)

      A stepper over the given range.

      Parameters
      • value: the initial value, clamped into the range

      • minValue: the lowest value the control will produce

      • maxValue: the highest value the control will produce

  • Method Details

    • getValue

      public int getValue()

      The current value.

      Returns

      the value, always within #getMinValue()..#getMaxValue()

    • setValue

      public void setValue(int newValue)

      Sets the value, clamped into the range. Fires the action listeners only when the value actually moved, so a caller that sets the same value twice does not report a change that did not happen.

      Parameters
      • newValue: the requested value
    • getMinValue

      public int getMinValue()

      The lowest value this control will produce.

      Returns

      the minimum

    • getMaxValue

      public int getMaxValue()

      The highest value this control will produce.

      Returns

      the maximum

    • setRange

      public void setRange(int minValue, int maxValue)

      Sets the range. The current value is clamped into the new range, which fires the listeners when that moves it.

      Parameters
      • minValue: the lowest value

      • maxValue: the highest value

    • getStep

      public int getStep()

      How far one press of a button moves the value.

      Returns

      the step, at least 1

    • setStep

      public void setStep(int step)

      Sets how far one press of a button moves the value.

      Parameters
      • step: the step, which must be positive
    • getDecrementButton

      public Button getDecrementButton()

      The decrement button, for a caller that wants to give it an icon.

      Returns

      the decrement button

    • getIncrementButton

      public Button getIncrementButton()

      The increment button, for a caller that wants to give it an icon.

      Returns

      the increment button

    • getField

      public TextField getField()

      The editable field, for a caller that wants to make it read-only.

      Returns

      the text field

    • addActionListener

      public void addActionListener(ActionListener l)

      Notified when the value changes, however it changed.

      Parameters
      • l: the listener
    • removeActionListener

      public void removeActionListener(ActionListener l)

      Stops notifying a listener.

      Parameters
      • l: the listener