欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Vaadin Web应用开发教程(16):UI组件-Checkbox

程序员文章站 2022-03-30 08:17:59
...

Checkbox 的状态可以为checked和unchecked. 其值可以通过getValue, setValue 来操作。基本用法如下

// A check box with default state (not checked, false).
final CheckBox checkbox1 = new CheckBox("My CheckBox");
main.addComponent(checkbox1);

// Another check box with explicitly set checked state.
final CheckBox checkbox2 = new CheckBox("Checked CheckBox");
checkbox2.setValue(true);
main.addComponent(checkbox2);

// Make some application logic. We use anonymous listener
// classes here. The above references were defined as final
// to allow accessing them from inside anonymous classes.
checkbox1.addListener(new ValueChangeListener() {
    public void valueChange(ValueChangeEvent event) {
        // Copy the value to the other checkbox.
        checkbox2.setValue(checkbox1.getValue());
    }
});
checkbox2.addListener(new ValueChangeListener() {
    public void valueChange(ValueChangeEvent event) {
        // Copy the value to the other checkbox.
        checkbox1.setValue(checkbox2.getValue());
    }
});
checkbox1.setImmediate(true);
checkbox2.setImmediate(true);

Vaadin Web应用开发教程(16):UI组件-Checkbox

Checkbox除了单独使用之外,还可以应用到OptionGroup 和Tabel中,具体参见后续文章。