Class Slider

All Implemented Interfaces:
ChangeHandler, ClickHandler, HasAllDragAndDropHandlers, HasAllFocusHandlers, HasAllGestureHandlers, HasAllKeyHandlers, HasAllMouseHandlers, HasAllTouchHandlers, HasBlurHandlers, HasChangeHandlers, HasClickHandlers, HasDoubleClickHandlers, HasDragEndHandlers, HasDragEnterHandlers, HasDragHandlers, HasDragLeaveHandlers, HasDragOverHandlers, HasDragStartHandlers, HasDropHandlers, HasFocusHandlers, HasGestureChangeHandlers, HasGestureEndHandlers, HasGestureStartHandlers, HasKeyDownHandlers, HasKeyPressHandlers, HasKeyUpHandlers, HasMouseDownHandlers, HasMouseMoveHandlers, HasMouseOutHandlers, HasMouseOverHandlers, HasMouseUpHandlers, HasMouseWheelHandlers, HasTouchCancelHandlers, HasTouchEndHandlers, HasTouchMoveHandlers, HasTouchStartHandlers, TouchEndHandler, TouchMoveHandler, TouchStartHandler, HasAttachHandlers, EventHandler, HasHandlers, EventListener, Focusable, HasEnabled, HasFocus, HasVisibility, IsWidget, SourcesClickEvents, SourcesFocusEvents, SourcesKeyboardEvents, SourcesMouseEvents, HasInputHandlers, InputHandler

A standard slider widget. The slider handles both linear and logarithmic scales.

CSS Style Rules

.gwt-Slider
the input element.

Example

 public class SliderExample implements EntryPoint {
        public void onModuleLoad() {
                // Make a new slider and display its value on label.
                VerticalPanel p = new VerticalPanel();
                Label l = new Label("Move slider...");
                Slider s = new Slider();
                s.addChangeHandler(new ChangeHandler() {
                        @Override
                        public void onChange(ChangeEvent event) {
                                l.setText("Slider at " + s.getValue());
                        }
                });
                p.add(l);
                p.add(s);
                // Add it to the root panel.
                RootPanel.get().add(p);
        }
 }
 
Author:
Christoph Hauert
  • Field Details

    • SLIDER_MIN

      private static final double SLIDER_MIN
      Default minimum value of slider.
      See Also:
    • SLIDER_MAX

      private static final double SLIDER_MAX
      Default maximum value of slider.
      See Also:
    • SLIDER_STEPS

      private static final int SLIDER_STEPS
      Default number of steps between minimum and maximum of slider.
      See Also:
    • slider

      protected HTML slider
      Reference to slider. The <input> element.
    • title

      protected String title
      Title of slider. This string is displayed as the slider's tooltip and is updated to show the slider's current value.
    • logBase

      protected double logBase
      For logarithmic sliders this is the base of the logarithm. For logBase≤0 the slider has a linear scale.
    • reversed

      protected boolean reversed
      true if minimum and maximum of slider are reversed.
    • min

      protected double min
      Minimum value of slider.
      See Also:
    • max

      protected double max
      Maximum value of slider.
      See Also:
    • steps

      protected int steps
      Number of steps between minimum and maximum value of slider.
      See Also:
    • logRange

      protected double logRange
      Helper variable to the logarithm of the slider range. Reduces calls to the fairly expensive Math.log(double).
  • Constructor Details

    • Slider

      public Slider()
      Creates a slider ranging from 0.0 to 100.0 with 100 steps and initial value (0.0+100.0)/2.
    • Slider

      public Slider(ChangeHandler handler)
      Creates a slider ranging from 0.0 to 100.0 with 100 steps and initial value (0.0+100.0)/2 as well as a change handler.
      Parameters:
      handler - the change handler
    • Slider

      public Slider(double min, double max)
      Creates a slider ranging from min to max with 100 steps and initial value (min+max)/2.
      Parameters:
      min - the minimum value
      max - the maximum value
    • Slider

      public Slider(double min, double max, ChangeHandler handler)
      Creates a slider ranging from min to max with 100 steps and initial value (min+max)/2 as well as a change handler.
      Parameters:
      min - the minimum value
      max - the maximum value
      handler - the change handler
    • Slider

      public Slider(double min, double max, double init, int steps)
      Creates a slider ranging from min to max with steps increments and initial value init.
      Parameters:
      min - the minimum value
      max - the maximum value
      init - the initial value
      steps - the number of steps
    • Slider

      public Slider(int min, int max, int init, int steps, ChangeHandler handler)
      Creates a slider ranging from min to max with steps increments and initial value init as well as a change handler.
      Parameters:
      min - the minimum value
      max - the maximum value
      init - the initial value
      steps - the number of steps
      handler - the change handler
    • Slider

      protected Slider(Element element)
      This constructor may be used by subclasses to explicitly use an existing element. This element must be a <input> element.
      Parameters:
      element - the element to be used
  • Method Details

    • wrap

      public static Slider wrap(Element element)
      Creates a Slider widget that wraps an existing <input> element. This element must already be attached to the document. If the element is removed from the document, you must call RootPanel.detachNow(Widget).
      Parameters:
      element - the element to be wrapped
      Returns:
      Slider widget wrapping element
    • getInputElement

      protected InputElement getInputElement()
      Get the input element underlying the slider.
      Returns:
      the InputElement
    • setRange

      public void setRange(double left, double right)
      Set left and right values of slider. Left values that exceed right values are acceptable.

      Note: HTML sliders must have the minimum on the left and the maximum on the right. This implementation automatically takes care of the conversion if the maximum is on the left and the minimum on the right.

      Parameters:
      left - the left slider value
      right - the right slider value
    • setSteps

      public void setSteps(int steps)
      Set the number of steps between the minimum and maximum values of the slider.
      Parameters:
      steps - the number of steps
    • setLogBase

      public void setLogBase(double base)
      Set the base for a logarithmic slider.
      Parameters:
      base - the logarithmic base of slider
      Throws:
      IllegalArgumentException - if base≤0 or the minimum and/or maximum values of the slider are ≤0
    • setLinear

      public void setLinear()
      Sets a linear scale for the slider.
    • _update

      private void _update()
      Updates the logRange if needed as well as the tooltip of the slider.
    • lin2log

      private double lin2log(double x)
      Helper function to convert from linear to logarithmic scales.
      Parameters:
      x - linear value
      Returns:
      logarithmic value
    • log2lin

      private double log2lin(double x)
      Helper function to convert from logarithmic to linear scales.
      Parameters:
      x - logarithmic value
      Returns:
      linear value
    • setEnabled

      public void setEnabled(boolean enabled)
      Specified by:
      setEnabled in interface HasEnabled
      Overrides:
      setEnabled in class FocusWidget
    • getValue

      public double getValue()
      Get the value of slider taking its linear or logarithmic scaling into account.
      Returns:
      current value of slider
    • setValue

      public void setValue(double value)
      Set the value of slider taking its linear or logarithmic scaling into account.
      Parameters:
      value - the new value of the slider
    • setTitle

      public void setTitle(String title)
      Overrides:
      setTitle in class UIObject
    • updateTitle

      protected void updateTitle()
      Updates the tooltip of the slider. By default it shows the sliders minimum, maximum and current value.
    • addInputHandler

      public HandlerRegistration addInputHandler(InputHandler handler)
      Description copied from interface: HasInputHandlers
      Adds a InputEvent handler.
      Specified by:
      addInputHandler in interface HasInputHandlers
      Parameters:
      handler - the input handler
      Returns:
      HandlerRegistration used to remove this handler
    • onInput

      public void onInput(InputEvent event)
      Description copied from interface: InputHandler
      Called when a InputEvent is fired.
      Specified by:
      onInput in interface InputHandler
      Parameters:
      event - the InputEvent that was fired
    • onClick

      public void onClick(ClickEvent event)
      Specified by:
      onClick in interface ClickHandler
    • onTouchStart

      public void onTouchStart(TouchStartEvent event)
      Specified by:
      onTouchStart in interface TouchStartHandler
    • onTouchMove

      public void onTouchMove(TouchMoveEvent event)
      Specified by:
      onTouchMove in interface TouchMoveHandler
    • onTouchEnd

      public void onTouchEnd(TouchEndEvent event)
      Specified by:
      onTouchEnd in interface TouchEndHandler
    • touchSlider

      private <H extends EventHandler> void touchSlider(TouchEvent<H> event)
      Helper function to update the slider value based on touch events.
      Type Parameters:
      H - the handler type
      Parameters:
      event - the touch event
    • addChangeHandler

      public HandlerRegistration addChangeHandler(ChangeHandler handler)
      Specified by:
      addChangeHandler in interface HasChangeHandlers
    • onChange

      public void onChange(ChangeEvent event)
      Specified by:
      onChange in interface ChangeHandler