简体   繁体   中英

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. Now the plan was to use the radio buttons to select a component. One RadioButton would select a ComboBox, the other a 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.

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. 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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