Class RangeSlider

java.lang.Object
com.codename1.ui.Component
com.codename1.components.RangeSlider
All Implemented Interfaces:
Animation, Editable, StyleListener

public class RangeSlider extends Component

A slider with two thumbs that selects a range, such as a price filter or a time window.

A price range slider with both thumbs

Form form = new Form("Filters", BoxLayout.y());
RangeSlider price = new RangeSlider(0, 500, 50, 300);
price.setStep(10);
Label shown = new Label("$50 - $300");
price.addActionListener(e -> shown.setText(
        "$" + price.getLowerValue() + " - $" + price.getUpperValue()));
form.add(price).add(shown);
form.show();

Dragging moves whichever thumb is nearer the touch; the thumbs never cross. Listeners fire while the value changes and once more when the drag ends, with #isAdjusting() false for that last event.

UIIDs: RangeSlider for the component. The fill takes the foreground of RangeSliderFill and the track the background of RangeSliderTrack when the theme defines them, and otherwise follow the theme's Slider colours.

  • Constructor Details

    • RangeSlider

      public RangeSlider()
      A slider over 0 to 100 selecting all of it.
    • RangeSlider

      public RangeSlider(int min, int max, int lower, int upper)

      A slider over min to max selecting lower to upper.

      Parameters
      • min: the smallest value

      • max: the largest value, greater than min

      • lower: the start of the selected range

      • upper: the end of the selected range

  • Method Details

    • initUnselectedStyle

      protected void initUnselectedStyle(Style unselectedStyle)
      Description copied from class: Component

      Can be overridden by subclasses to perform initialization when the unselected style is set to a new value.

      Parameters
      • unselectedStyle: The unselected style.
      Overrides:
      initUnselectedStyle in class Component
    • getMinValue

      public int getMinValue()

      The smallest value.

      Returns

      the minimum

    • getMaxValue

      public int getMaxValue()

      The largest value.

      Returns

      the maximum

    • getLowerValue

      public int getLowerValue()

      The start of the selected range.

      Returns

      the lower value

    • getUpperValue

      public int getUpperValue()

      The end of the selected range.

      Returns

      the upper value

    • setRange

      public final void setRange(int lower, int upper)

      Sets the selected range. Values are clamped to the slider's bounds and snapped to the step, and swapped if given in the wrong order.

      Parameters
      • lower: the start of the range

      • upper: the end of the range

    • setBounds

      public void setBounds(int min, int max)

      Sets the slider's bounds, keeping the selection inside them.

      Parameters
      • min: the smallest value

      • max: the largest value, greater than min

    • getStep

      public int getStep()

      The increment the values snap to.

      Returns

      the step, 1 by default

    • setStep

      public void setStep(int step)

      Sets the increment the values snap to, counted from the minimum.

      Parameters
      • step: a positive step
    • isAdjusting

      public boolean isAdjusting()

      True while the user is dragging a thumb. Listeners hear every change during a drag with this true, then once more with it false when the finger lifts; a listener that runs a search can ignore the first kind.

      Returns

      whether a drag is in progress

    • addActionListener

      public void addActionListener(ActionListener l)

      Adds a listener called whenever the range changes.

      Parameters
      • l: the listener
    • removeActionListener

      public void removeActionListener(ActionListener l)

      Removes a listener.

      Parameters
      • l: the listener
    • calcPreferredSize

      protected Dimension calcPreferredSize()
      Description copied from class: Component

      Calculates the preferred size based on component content. This method is invoked lazily by getPreferred size.

      Returns

      the calculated preferred size based on component content

      Overrides:
      calcPreferredSize in class Component
    • paint

      public void paint(Graphics g)
      Description copied from class: Component

      This method paints the Component on the screen, it should be overriden by subclasses to perform custom drawing or invoke the UI API's to let the PLAF perform the rendering.

      Parameters
      • g: the component graphics
      Specified by:
      paint in interface Animation
      Overrides:
      paint in class Component
    • pointerPressed

      public void pointerPressed(int x, int y)
      Description copied from class: Component

      If this Component is focused, the pointer pressed event will call this method

      Parameters
      • x: the pointer x coordinate

      • y: the pointer y coordinate

      Overrides:
      pointerPressed in class Component
    • pointerDragged

      public void pointerDragged(int x, int y)
      Description copied from class: Component

      If this Component is focused, the pointer dragged event will call this method

      Parameters
      • x: the pointer x coordinate

      • y: the pointer y coordinate

      Overrides:
      pointerDragged in class Component
    • pointerReleased

      public void pointerReleased(int x, int y)
      Description copied from class: Component

      If this Component is focused, the pointer released event will call this method

      Parameters
      • x: the pointer x coordinate

      • y: the pointer y coordinate

      Overrides:
      pointerReleased in class Component
    • deinitialize

      protected void deinitialize()
      Description copied from class: Component
      Invoked to indicate that the component initialization is being reversed since the component was detached from the container hierarchy. This allows the component to deregister animators and cleanup after itself. This method is the opposite of the initComponent() method.
      Overrides:
      deinitialize in class Component
    • isStickyDrag

      protected boolean isStickyDrag()
      Description copied from class: Component

      Returns true if the component is interested in receiving drag/pointer release events even after the gesture exceeded its boundaries. This is useful for spinners etc. where the motion might continue beyond the size of the component

      Returns

      false by default

      Overrides:
      isStickyDrag in class Component
    • keyReleased

      public void keyReleased(int keyCode)
      Description copied from class: Component

      If this Component is focused, the key released event will call this method

      Parameters
      • keyCode: the key code value to indicate a physical key.
      Overrides:
      keyReleased in class Component