繁体   English   中英

Vaadin:如何使用 RadioButton 使组件可选?

[英]Vaadin: How do I make a Component selectable with a RadioButton?

我有一个用例,用户需要 select 带有复选框的项目,然后可以选择两个单选按钮。 现在的计划是使用单选按钮来 select 一个组件。 一个 RadioButton 是 select 和 ComboBox,另一个是 TextField。 但是,我无法完成这项工作,也没有在谷歌搜索时找到任何信息。

我错过了什么吗? 如果我使用 setItems(),我会得到 object 参考,而不是 object 本身。

谢谢!

编辑 - 我尝试过的代码:

RadioButtonGroup<Component> rbg = new RadioButtonGroup();
TextField tf = new TextField("foo");
ComboBox<String> cb = new ComboBox();
rbg.setItems(tf, cb);

RadioButtonGroup<Component> rbg = new RadioButtonGroup();
TextField tf = new TextField("foo");
ComboBox<String> cb = new ComboBox();
rbg.add(tf, cb);

我尝试使用不同的类型和方法,但没有成功。

您不能将<Component>用作字段的泛型类型参数。 你需要这样的东西:

        RadioButtonGroup<String> radioButtonGroup = new RadioButtonGroup<>();
        radioButtonGroup.setItems("foo", "bar");
        radioButtonGroup.setRenderer(new ComponentRenderer<>(item -> {
            if ("foo".equals(item)) {
                TextField textField = new TextField();
                return textField;
            } else {
                ComboBox<String> comboBox = new ComboBox<>();
                comboBox.setItems("baz", "quux");
                return comboBox;
            }
        }));
        radioButtonGroup.setValue("foo");
        add(radioButtonGroup);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM