Here is what you have to do to properly use a CheckboxGroup:
In your class that you want to bind (Person in this case), the used property must be a Set.
in the view, define a CheckboxGroup<Phone> and set the possible options (list of phones) with checkboxGroup.setDataProvider(allOptions);
Then bind the Person to the checkboxGroup using either one of these two methods:
binder.bind(checkboxGroup, "phones");
// or
binder.forField(checkboxGroup)
// you can add converters and or validators here
.bind(Person::getPhones, Person::setPhones);
Here is an example code of mine showing what I described above:
@Route(value = "test)
public class TestView extends VerticalScrollLayout {
public TestView(){
add(new H4("Test View"));
Person person = new Person();
Binder<Person> binder = new Binder<>(Person.class);
binder.setBean(person);
CheckboxGroup<Phone> checkboxGroup = new CheckboxGroup<>();
// provide all options - I use an enum, but you could also provide real objects
checkboxGroup.setDataProvider(new ListDataProvider<>(Arrays.asList(Phone.values())));
//binder.forField(checkboxGroup).bind(Person::getPhones, Person::setPhones);
binder.bind(checkboxGroup, "phones");
add(checkboxGroup);
// Button to show current selection
Button showSelectedPhones = new Button("show current selection", click -> {
// to proof that the binder works, I read the values from the bound person object
String selectedPhonesString = person.getPhones().stream().map(Object::toString).collect(Collectors.joining(", "));
Notification.show("The person has these phones: "+selectedPhonesString);
});
add(showSelectedPhones);
}
public class Person {
private Set<Phone> phones;
public Person(){
}
public Set<Phone> getPhones() {
return phones;
}
public void setPhones(Set<Phone> phones) {
this.phones = phones;
}
}
public enum Phone {
MOBILE,
LANDLINE,
BUSINESS;
}
}