Search code examples
vaadinvaadin24

Vaadin 24 - Problem Binding ENUM Roles to CheckboxGroup


I'm new to both Vaadin and Java - so sorry if this is a stupid question. I'm using the User and Role entities from the default starter - and I'm trying to create a checkbox group of the enum roles - but I'm banging my head against a wall.

Here's what I've tried:

CheckBoxGroup<Role> roleCheckBoxGroup = new CheckBoxGroup<>("Roles", DataProvider.ofItems(Role.values()));

Result: The IDE complains and wants me to create a new class "CheckBoxGroup" (even though I've already imported the Vaadin Component - import com.vaadin.flow.component.checkbox.CheckboxGroup).

Next up:

CheckboxGroup<Role> roleCheckbox = new CheckboxGroup<>();
 roleCheckbox.setDataProvider(new ListDataProvider<>(Arrays.asList(Phone.values())));

Result: The IDE doesn't know what "asList" means and wants to search the Maven repository.

I feel like I'm "close" - but these are literally the only two examples I could find on the Interwebs.

I would really appreciate it if one of you kind folks could point me in the right direction.

BTW: I did review: Binding List<Enum> to CheckBoxGroup in vaadin - that was one of the two examples. It appears as if it doesn't work for Vaadin 24.


Solution

  • I solved it:

    CheckboxGroup<Role> roleCheckbox = new CheckboxGroup<>();
    roleCheckbox.setItems(Role.values());
    binder.forField(roleCheckbox).bind(User::getRoles, User::setRoles);
    

    I hope this helps someone in the future!