Error in Vaadin Radio Button Example

RadioButtonGroup<Department> radioGroup = new RadioButtonGroup<>();
radioGroup.setLabel("Department");
List<Department> departmentList = getDepartments();
radioGroup.setItems(departmentList);
radioGroup.setRenderer(new TextRenderer<>(Department::getName));
radioGroup.addThemeVariants(RadioGroupVariant.LUMO_VERTICAL);

The Vaadin “Entity list” example for [Radio Button]
(https://vaadin.com/components/vaadin-radio-button/java-examples) shows a radio button group with nothing selected. I’m using version 14.1.18 and running the exact same code (as shown above) will not create a radio button group that has nothing selected. By default, the first item in the list gets selected automatically. Either the implementation has changed and Vaadin never updated the documentation or that’s not the code that was used to create that example.

I just tested this on a Vaadin 14.1.18 project and for me it works exactly the same as on vaadin.com. Everything is unselected.

Btw vaadin.com also seems to be running 14.1.18 atm. (that version includes vaadin-radio-button web component version [1.2.4]
(https://github.com/vaadin/vaadin-radio-button/releases/tag/v1.2.4) and vaadin-radio-button-flow version [2.0.2]
(https://github.com/vaadin/vaadin-radio-button-flow/releases/tag/2.0.2)).

You can see the actual source of that demo (for that component version) here: https://github.com/vaadin/vaadin-radio-button-flow/blob/2.0.2/vaadin-radio-button-flow-demo/src/main/java/com/vaadin/flow/component/radiobutton/demo/RadioButtonGroupView.java#L69-L81
where you can also see the implementation of the [getDepartments()]
(https://github.com/vaadin/vaadin-radio-button-flow/blob/2.0.2/vaadin-radio-button-flow-demo/src/main/java/com/vaadin/flow/component/radiobutton/demo/RadioButtonGroupView.java#L106-L110), [Department]
(https://github.com/vaadin/vaadin-radio-button-flow/blob/2.0.2/vaadin-radio-button-flow-demo/src/main/java/com/vaadin/flow/component/radiobutton/demo/entity/Department.java) and [DepartmentData]
(https://github.com/vaadin/vaadin-radio-button-flow/blob/2.0.2/vaadin-radio-button-flow-demo/src/main/java/com/vaadin/flow/component/radiobutton/demo/data/DepartmentData.java)

I also tried with Vaadin 14.1.21 and it still works the same (all options unselected).

I don’t understand why the code works for the values used in the example, but doesn’t work for other values.

public enum Department {PRODUCT, SERVICES, HR, ACCOUNTING;
    public static String getName(Department department) {
        switch (department) {
            case PRODUCT:
                return "Product";
            case SERVICES:
                return "Services";
            case HR:
                return "HR";
            case ACCOUNTING:
                return "Accounting";
        }
        return "";
    }
}
public enum Sex {MALE, FEMALE;
    public static String getName(Sex sex) {
        return sex == MALE ? "Male" : "Female";
    }
}
private final RadioButtonGroup<Department> radioGroup;
private final RadioButtonGroup<Sex> sex;

radioGroup = new RadioButtonGroup<>();
radioGroup.setLabel("Department");
radioGroup.setItems(new ArrayList<>(Arrays.asList(PRODUCT,SERVICES,HR,ACCOUNTING)));
radioGroup.setRenderer(new TextRenderer<>(Department::getName));

sex = new RadioButtonGroup<>();
sex.setLabel("Sex");
sex.setItems(new ArrayList<>(Arrays.asList(MALE, FEMALE)));
sex.setRenderer(new TextRenderer<>(Sex::getName));

I’m doing the exact same thing with both radio button groups, and yet, one has a completely different behavior than the other.

18181979.png

I tried with your code for the Sex enum and radio button group, but it still works for me without having anything automatically selected.

Can you share a full project zip where this issue happens for easier debugging and what might be going wrong in your project?

I finally got it to work, but it was a laborious process:
Step 1: delete the sex radio button group and recompile
Step 2. rename the variable and recompile
Step 3. rename the enum and recompile
Step 4. change the enum values and recompile
Step 5. change the implementation of the getName method (simplify) and recompile

I have no idea why I was getting different behavior from the sex radio button group for the first 10 times I tried to get it working. I would send a zip, but I don’t want to roll back all of the changes I’ve made since then.

Great to hear that you got it working. Did you try running mvn clean at any point?

I finally thought of ‘mvn clean’ after about 5 tries. I have no idea what the issue could have been. The Vaadin example worked as soon as I tried it. The code that I couldn’t get to work was, for all practical purposes, the same code but with my user defined types.