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 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. ---
  2. title: Slider
  3. order: 28
  4. layout: page
  5. ---
  6. [[components.slider]]
  7. = [classname]#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(SliderOrientation.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]#SliderOrientation.HORIZONTAL#) or vertical ([parameter]#SliderOrientation.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. vertvalue.setSizeUndefined();
  39. // Handle changes in slider value.
  40. vertslider.addValueChangeListener(
  41. new Property.ValueChangeListener() {
  42. public void valueChange(ValueChangeEvent event) {
  43. double value = (Double) vertslider.getValue();
  44. // Use the value
  45. box.setHeight((float) value, Sizeable.UNITS_PERCENTAGE);
  46. vertvalue.setValue(String.valueOf(value));
  47. }
  48. });
  49. // The slider has to be immediate to send the changes
  50. // immediately after the user drags the handle.
  51. vertslider.setImmediate(true);
  52. ----
  53. You can set the value with the [methodname]#setValue()# method defined in
  54. [classname]#Slider# that takes the value as a native double value. The setter
  55. can throw a [classname]#ValueOutOfBoundsException#, which you must handle.
  56. [source, java]
  57. ----
  58. // Set the initial value. This has to be set after the
  59. // listener is added if we want the listener to handle
  60. // also this value change.
  61. try {
  62. vertslider.setValue(50.0);
  63. } catch (ValueOutOfBoundsException e) {
  64. }
  65. ----
  66. Alternatively, you can use the regular [methodname]#setValue(Object)#, which
  67. does not do bounds checking.
  68. <<figure.components.slider.example1>> shows both vertical (from the code
  69. examples) and horizontal sliders that control the size of a box. The slider
  70. values are displayed also in separate labels.
  71. == CSS Style Rules
  72. [source, css]
  73. ----
  74. .v-slider {}
  75. .v-slider-base {}
  76. .v-slider-handle {}
  77. ----
  78. The enclosing style for the [classname]#Slider# is [literal]#++v-slider++#. The
  79. slider bar has style [literal]#++v-slider-base++#. Even though the handle is
  80. higher (for horizontal slider) or wider (for vertical slider) than the bar, the
  81. handle element is nevertheless contained within the slider bar element. The
  82. appearance of the handle comes from a background image defined in the
  83. __background__ CSS property.