Slider 組件可以顯示為垂直或是水平滑動條,可以使用鼠標(biāo)拖動來設(shè)置其值。 其基本使用如下:
// 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);
http://wiki.jikexueyuan.com/project/vaadin-web-development-tutorial/images/67.png" alt="" />
Slider 組件也屬于 Field 組件,因此可以通過 ValueChangeListener 來監(jiān)聽 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) {
}