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 (simple About view and no menus with Access Control set to “Users who have logged in”) - and I’m trying to create a checkbox group of the enum roles - but I’m banging my head against a wall.

I can’t seem to:

  1. Read the Role enum
  2. Put it into thte proper format for CheckboxGroup.setItems()
  3. Have the values auto-select when the User is selected from the grid

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: https://stackoverflow.com/questions/46863202/binding-listenum-to-checkboxgroup-in-vaadin - that was one of the two examples. It appears as if it doesn’t work for Vaadin 24.

You using Intellij? Sometimes it needs some love - re-opening / reimporting the project if it is not smart enough to read dependencies

Looking a second time: your constructor looks way to complicated

new CheckBoxGroup<>(“Roles”, Role.values());

@quirky-zebra - thanks for the quick reply. I’m using NetBeans (I know, I know, but it’s the company standard). when I try to use CheckboxGroup<String> roleCheckbox = new CheckBoxGroup<>("Roles", Roles.values()); I get the same result: The IDE complains and wants me to create a new class “CheckBoxGroup”.

Using this:

CheckboxGroup<String> roleCheckbox = new CheckboxGroup<>();
roleCheckbox.setItems("ADMIN","USER");

Happy as a clam. I was just wondering if there was a way to read the Role entity (enum) and have it populate the items - rather than hard-coding them.

in your first example is wrong - it has to be

I solved it:

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

I hope this helps someone in the future!

@quirky-zebra - thanks very much for your help!