简体   繁体   English

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

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

I have a use case where the user needs to select items with checkboxes and then has a choice of two radio buttons.我有一个用例,用户需要 select 带有复选框的项目,然后可以选择两个单选按钮。 Now the plan was to use the radio buttons to select a component.现在的计划是使用单选按钮来 select 一个组件。 One RadioButton would select a ComboBox, the other a TextField.一个 RadioButton 是 select 和 ComboBox,另一个是 TextField。 However, I can't make this work nor did I find any info when googling.但是,我无法完成这项工作,也没有在谷歌搜索时找到任何信息。

Am I missing something?我错过了什么吗? If I use setItems(), I get the object reference, not the object itself.如果我使用 setItems(),我会得到 object 参考,而不是 object 本身。

Thank you!谢谢!

Edit - Code which I tried:编辑 - 我尝试过的代码:

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

And

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

I tried to play around with different types and methods, but to no luck.我尝试使用不同的类型和方法,但没有成功。

You can't use <Component> as the generic type parameter for a field.您不能将<Component>用作字段的泛型类型参数。 You'll need something like this:你需要这样的东西:

        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