clickguy
clickguy

Reputation: 91

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.

Upvotes: 0

Views: 201

Answers (2)

clickguy
clickguy

Reputation: 91

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!

Upvotes: 1

Tatu Lund
Tatu Lund

Reputation: 10643

Value of the CheckBoxGroup and all other multiselect fields in Vaadin is Set. Thus you can't directly bind it to property of type List. So you either need to change the property type or use withConverter and write converter from presentation type Set to model type List.

Upvotes: 1

Related Questions