Slider组件可以显示为垂直或是水平滑动条,可以使用鼠标拖动来设置其值。 其基本使用如下:
// Create a vertical slider final Slider vertslider = new Slider(1, 100); vertslider.setOrientation(Slider.ORIENTATION_HORIZONTAL); // Shows the value of the vertical slider final Label vertvalue = new Label(); vertvalue.setSizeUndefined(); // Handle changes in slider value. vertslider.addListener(new Property.ValueChangeListener() { public void valueChange( com.vaadin.data.Property.ValueChangeEvent event) { double value = (Double) vertslider.getValue(); vertvalue.setValue(String.valueOf(value)); } }); // The slider has to be immediate to send the changes // immediately after the user drags the handle. vertslider.setImmediate(true);
Slider组件也属于Field组件,因此可以通过ValueChangeListener来监听Slider组件值的变化。同样可以使用setValue 来修改Slider组件的值,此时要注意Catch可能的ValueOutOfBoundsException。
// Set the initial value. This has to be set after the // listener is added if we want the listener to handle // also this value change. try { vertslider.setValue(50.0); } catch (ValueOutOfBoundsException e) { }