簡體   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