Class Slider

All Implemented Interfaces:
AttachNotifier, BlurNotifier<Slider>, DetachNotifier, Focusable<Slider>, FocusNotifier<Slider>, HasAriaLabel, HasElement, HasEnabled, HasHelper, HasLabel, HasSize, HasStyle, HasValidation, HasValue<AbstractField.ComponentValueChangeEvent<Slider,Double>,Double>, HasValueAndElement<AbstractField.ComponentValueChangeEvent<Slider,Double>,Double>, KeyNotifier, HasTooltip, HasValidationProperties, InputField<AbstractField.ComponentValueChangeEvent<Slider,Double>,Double>, HasValueChangeMode, Serializable

@Tag("vaadin-slider") @NpmPackage(value="@vaadin/slider", version="25.1.0-alpha7") @JsModule("@vaadin/slider/src/vaadin-slider.js") public class Slider extends AbstractSinglePropertyField<Slider,Double> implements HasAriaLabel
Slider is an input field that allows the user to select a numeric value within a range by dragging a handle along a track or using arrow keys for precise input.
Author:
Vaadin Ltd.
See Also:
  • Constructor Details

    • Slider

      public Slider()
      Constructs a Slider with min 0, max 100, and initial value 0.

      The step defaults to 1.

    • Slider

      Constructs a Slider with min 0, max 100, initial value 0, and a value change listener.

      The step defaults to 1.

      Parameters:
      listener - the value change listener
    • Slider

      public Slider(double min, double max, double value)
      Constructs a Slider with the given min, max and initial value.

      The step defaults to 1.

      Parameters:
      min - the minimum value
      max - the maximum value
      value - the initial value
    • Slider

      public Slider(double min, double max, double value, HasValue.ValueChangeListener<? super AbstractField.ComponentValueChangeEvent<Slider,Double>> listener)
      Constructs a Slider with the given min, max, initial value, and a value change listener.

      The step defaults to 1.

      Parameters:
      min - the minimum value
      max - the maximum value
      value - the initial value
      listener - the value change listener
    • Slider

      public Slider(double min, double max, double step, double value)
      Constructs a Slider with the given min, max, step, and initial value.
      Parameters:
      min - the minimum value
      max - the maximum value
      step - the step value
      value - the initial value
    • Slider

      public Slider(double min, double max, double step, double value, HasValue.ValueChangeListener<? super AbstractField.ComponentValueChangeEvent<Slider,Double>> listener)
      Constructs a Slider with the given min, max, step, initial value, and a value change listener.
      Parameters:
      min - the minimum value
      max - the maximum value
      step - the step value
      value - the initial value
      listener - the value change listener
    • Slider

      public Slider(String label)
      Constructs a Slider with the given label, min 0, max 100, and initial value 0.

      The step defaults to 1.

      Parameters:
      label - the text to set as the label
    • Slider

      Constructs a Slider with the given label, min 0, max 100, initial value 0, and a value change listener.

      The step defaults to 1.

      Parameters:
      label - the text to set as the label
      listener - the value change listener
    • Slider

      public Slider(String label, double min, double max, double value)
      Constructs a Slider with the given label, min, max, and initial value.

      The step defaults to 1.

      Parameters:
      label - the text to set as the label
      min - the minimum value
      max - the maximum value
      value - the initial value
    • Slider

      public Slider(String label, double min, double max, double value, HasValue.ValueChangeListener<? super AbstractField.ComponentValueChangeEvent<Slider,Double>> listener)
      Constructs a Slider with the given label, min, max, initial value, and a value change listener.
      Parameters:
      label - the text to set as the label
      min - the minimum value
      max - the maximum value
      value - the initial value
      listener - the value change listener
    • Slider

      public Slider(String label, double min, double max, double step, double value)
      Constructs a Slider with the given label, min, max, step, and initial value.
      Parameters:
      label - the text to set as the label
      min - the minimum value
      max - the maximum value
      step - the step value
      value - the initial value
    • Slider

      public Slider(String label, double min, double max, double step, double value, HasValue.ValueChangeListener<? super AbstractField.ComponentValueChangeEvent<Slider,Double>> listener)
      Constructs a Slider with the given label, min, max, step, initial value, and a value change listener.
      Parameters:
      label - the text to set as the label
      min - the minimum value
      max - the maximum value
      step - the step value
      value - the initial value
      listener - the value change listener
  • Method Details

    • setAriaLabel

      public void setAriaLabel(String ariaLabel)
      Sets an accessible name for the range input element of the slider.
      Specified by:
      setAriaLabel in interface HasAriaLabel
      Parameters:
      ariaLabel - the accessible name to set, or null to remove it
    • getAriaLabel

      public Optional<String> getAriaLabel()
      Gets the accessible name for the range input element of the slider.
      Specified by:
      getAriaLabel in interface HasAriaLabel
      Returns:
      an optional accessible name, or an empty optional if no accessible name has been set
    • setAriaLabelledBy

      public void setAriaLabelledBy(String ariaLabelledBy)
      Sets the id of an element to be used as the accessible name for the range input element of the slider.
      Specified by:
      setAriaLabelledBy in interface HasAriaLabel
      Parameters:
      ariaLabelledBy - the id of the element to be used as the label, or null to remove it
    • getAriaLabelledBy

      public Optional<String> getAriaLabelledBy()
      Gets the id of the element used as the accessible name for the range input element of the slider.
      Specified by:
      getAriaLabelledBy in interface HasAriaLabel
      Returns:
      an optional id of the element used as the label, or an empty optional if no id has been set
    • getMin

      public double getMin()
      Gets the minimum value of the slider.
      Returns:
      the minimum value
    • setMin

      public void setMin(double min)
      Sets the minimum value of the slider.

      This method automatically clamps the current value to be no less than the new minimum, which may trigger a value change event. To set the value explicitly along with the new minimum, use the

      invalid reference
      setValue(value, min, max)
      method instead.
      Parameters:
      min - the minimum value
      Throws:
      IllegalArgumentException - if min is greater than the current max
    • getMax

      public double getMax()
      Gets the maximum value of the slider.
      Returns:
      the maximum value
    • setMax

      public void setMax(double max)
      Sets the maximum value of the slider.

      This method automatically clamps the current value to be no greater than the new maximum, which may trigger a value change event. To set the value explicitly along with the new maximum, use the

      invalid reference
      setValue(value, min, max)
      method instead.
      Parameters:
      max - the maximum value
      Throws:
      IllegalArgumentException - if max is less than the current min
    • getStep

      public double getStep()
      Gets the step value of the slider.

      Valid slider values are calculated relative to the minimum value: min, min + step, min + 2*step, etc.

      Returns:
      the step value
    • setStep

      public void setStep(double step)
      Sets the step value of the slider.

      This method automatically adjusts the current value to be aligned with the new step, which may trigger a value change event. To set the value explicitly along with the new step, use the

      invalid reference
      setValue(value, min,
       max, step)
      method instead.
      Parameters:
      step - the step value
      Throws:
      IllegalArgumentException - if step is not positive
    • clear

      public void clear()
      Clears the slider value, setting it to the minimum value.
      Specified by:
      clear in interface HasValue<AbstractField.ComponentValueChangeEvent<Slider,Double>,Double>
      See Also:
    • hasValidValue

      protected boolean hasValidValue()
      Description copied from class: AbstractSinglePropertyField
      Checks whether the element property has a value that can be converted to the model type. Property changes from the element will be ignored if this method returns false. The default implementation always return true.
      Overrides:
      hasValidValue in class AbstractSinglePropertyField<Slider,Double>
      Returns:
      true if the element property value can be converted to the model type; otherwise false
    • onAttach

      protected void onAttach(AttachEvent attachEvent)
      Description copied from class: Component
      Called when the component is attached to a UI.

      This method is invoked before the AttachEvent is fired for the component. Make sure to call super.onAttach when overriding this method.

      Overrides:
      onAttach in class Component
      Parameters:
      attachEvent - the attach event
    • setValueAlwaysVisible

      public void setValueAlwaysVisible(boolean valueAlwaysVisible)
      Sets whether the value bubble is always visible, regardless of focus or hover state. By default, bubble is hidden and only shown on interaction.
      Parameters:
      valueAlwaysVisible - true to always show the value bubble, false otherwise
    • isValueAlwaysVisible

      public boolean isValueAlwaysVisible()
      Gets whether the value bubble is always visible, regardless of focus or hover state. By default, bubble is hidden and only shown on interaction.
      Returns:
      true if the value bubble is always visible, false otherwise
    • setMinMaxVisible

      public void setMinMaxVisible(boolean minMaxVisible)
      Sets whether the min and max values are displayed below the slider track. By default, min and max values are hidden.
      Parameters:
      minMaxVisible - true to display min and max values, false otherwise
    • isMinMaxVisible

      public boolean isMinMaxVisible()
      Gets whether the min and max values are displayed below the slider track. By default, min and max values are hidden.
      Returns:
      true if the min and max values are displayed, false otherwise
    • setValue

      public void setValue(Double value)
      Sets the value of the slider.
      Specified by:
      setValue in interface HasValue<TComponent extends com.vaadin.flow.component.slider.SliderBase<TComponent,TValue>,TValue>
      Overrides:
      setValue in class AbstractField<TComponent extends com.vaadin.flow.component.slider.SliderBase<TComponent,TValue>,TValue>
      Parameters:
      value - the value
      Throws:
      IllegalArgumentException - if value is not valid for the current min, max and step
    • setValue

      public void setValue(Double value, double min, double max)
      Sets the minimum, maximum, and value of the slider atomically.

      The step remains unchanged.

      Parameters:
      value - the value
      min - the minimum value
      max - the maximum value
      Throws:
      IllegalArgumentException - if min is greater than max
      IllegalArgumentException - if value is not valid for the given min, max and current step
    • setValue

      public void setValue(Double value, double min, double max, double step)
      Sets the minimum, maximum, step, and value of the slider atomically.
      Parameters:
      value - the value
      min - the minimum value
      max - the maximum value
      step - the step value
      Throws:
      IllegalArgumentException - if min is greater than max
      IllegalArgumentException - if step is not positive
      IllegalArgumentException - if value is not valid for the given min, max and step
    • getValueChangeMode

      public ValueChangeMode getValueChangeMode()
      Gets current value change mode of the component.

      The default value is ValueChangeMode.ON_CHANGE.

      Specified by:
      getValueChangeMode in interface HasValueChangeMode
      Returns:
      current value change mode of the component, or null if the value is not synchronized
    • setValueChangeMode

      public void setValueChangeMode(ValueChangeMode valueChangeMode)
      Description copied from interface: HasValueChangeMode
      Sets new value change mode for the component.
      Specified by:
      setValueChangeMode in interface HasValueChangeMode
      Parameters:
      valueChangeMode - new value change mode, or null to disable the value synchronization
    • setValueChangeTimeout

      public void setValueChangeTimeout(int valueChangeTimeout)
      Description copied from interface: HasValueChangeMode
      Sets how often HasValue.ValueChangeEvents are triggered when the ValueChangeMode is set to ValueChangeMode.LAZY, or ValueChangeMode.TIMEOUT.

      Implementations should use ValueChangeMode.applyChangeTimeout(ValueChangeMode, int, DomListenerRegistration).

      Specified by:
      setValueChangeTimeout in interface HasValueChangeMode
      Parameters:
      valueChangeTimeout - the timeout in milliseconds of how often HasValue.ValueChangeEvents are triggered.
    • getValueChangeTimeout

      public int getValueChangeTimeout()
      Description copied from interface: HasValueChangeMode
      Returns the currently set timeout, for how often HasValue.ValueChangeEvents are triggered when the ValueChangeMode is set to ValueChangeMode.LAZY, or ValueChangeMode.TIMEOUT.
      Specified by:
      getValueChangeTimeout in interface HasValueChangeMode
      Returns:
      the timeout in milliseconds of how often HasValue.ValueChangeEvents are triggered.