You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

components-slider.asciidoc 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. ---
  2. title: Slider
  3. order: 28
  4. layout: page
  5. ---
  6. [[components.slider]]
  7. = Slider
  8. ifdef::web[]
  9. [.sampler]
  10. image:{live-demo-image}[alt="Live Demo", link="http://demo.vaadin.com/sampler/#ui/data-input/other/slider"]
  11. endif::web[]
  12. The [classname]#Slider# is a vertical or horizontal bar that allows setting a
  13. numeric value within a defined range by dragging a bar handle with the mouse.
  14. The value is shown when dragging the handle.
  15. [[figure.components.slider.example1]]
  16. .Vertical and horizontal [classname]#Slider# components
  17. image::img/slider-example1-hi.png[width=40%, scaledwidth=70%]
  18. [classname]#Slider# has a number of different constructors that take a
  19. combination of the caption, _minimum_ and _maximum_ value, _resolution_,
  20. and the _orientation_ of the slider.
  21. [source, java]
  22. ----
  23. // Create a vertical slider
  24. Slider vertslider = new Slider(1, 100);
  25. vertslider.setOrientation(Orientation.VERTICAL);
  26. ----
  27. __min__:: Minimum value of the slider range. The default is 0.0.
  28. __max__:: Maximum value of the slider range. The default is 100.0.
  29. __resolution__:: The number of digits after the decimal point. The default is 0.
  30. __orientation__:: The orientation can be either horizontal ([parameter]#Orientation.HORIZONTAL#) or vertical ([parameter]#Orientation.VERTICAL#). The default is horizontal.
  31. As the [classname]#Slider# is a field component, you can handle value changes
  32. with a [classname]#ValueChangeListener#. The value of the [classname]#Slider#
  33. field is a [classname]#Double# object.
  34. [source, java]
  35. ----
  36. // Shows the value of the vertical slider
  37. final Label vertvalue = new Label();
  38. // Handle changes in slider value.
  39. vertslider.addValueChangeListener(event -> {
  40. float value = event.getValue().floatValue();
  41. box.setHeight(value, Unit.PERCENTAGE);
  42. vertvalue.setValue(String.valueOf(value));
  43. });
  44. ----
  45. You can set the value with the [methodname]#setValue()# method defined in
  46. [classname]#Slider# that takes the value as a [classname]#Double#. If the value
  47. is outside the configured bounds, the setter throws a [classname]#ValueOutOfBoundsException#.
  48. [source, java]
  49. ----
  50. // Set the initial value. This has to be set after the
  51. // listener is added if we want the listener to handle
  52. // also this value change.
  53. try {
  54. vertslider.setValue(50.0);
  55. } catch (ValueOutOfBoundsException e) {
  56. }
  57. ----
  58. <<figure.components.slider.example1>> shows both vertical (from the code
  59. examples) and horizontal sliders that control the size of a box. The slider
  60. values are displayed also in separate labels.
  61. == CSS Style Rules
  62. [source, css]
  63. ----
  64. .v-slider {}
  65. .v-slider-base {}
  66. .v-slider-handle {}
  67. ----
  68. The enclosing style for the [classname]#Slider# is [literal]#++v-slider++#. The
  69. slider bar has style [literal]#++v-slider-base++#. Even though the handle is
  70. higher (for horizontal slider) or wider (for vertical slider) than the bar, the
  71. handle element is nevertheless contained within the slider bar element. The
  72. appearance of the handle comes from a background image defined in the
  73. __background__ CSS property.