Important Notice - Forums is archived
To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.
Vaadin 8 Binding Custom Business Object to CheckBoxGroup
Hello Everyone,
I'm upgrading one of my Vaadin projects to version 8 and I'm facing a problem when binding a custom Business Object to a CheckBoxGroup. I simplified my project into the following test scenario:
Class Person:
package com.vaadin.test;
import java.util.Date;
import java.util.Set;
public class Person {
private String name;
private Set<Tag> tags;
public Person(String name) {
super();
this.name = name;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Set<Tag> getTags() {
return tags;
}
public void setTags(Set<Tag> tags) {
this.tags = tags;
}
}
An object of the class "Person" can have many Tags. The class Tag looks like this:
package com.vaadin.test;
public class Tag {
private String name;
public Tag(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return name;
}
}
I created a simple UI with a TextField, a CheckBoxGroup and a Binder:
package com.vaadin.test;
import java.util.HashSet;
import com.vaadin.annotations.Theme;
import com.vaadin.data.Binder;
import com.vaadin.server.VaadinRequest;
import com.vaadin.ui.CheckBoxGroup;
import com.vaadin.ui.Label;
import com.vaadin.ui.TextField;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
@Theme("valo")
public class TestUI extends UI {
private static final long serialVersionUID = 1L;
@Override
protected void init(VaadinRequest request) {
Person person = new Person("Marcus");
person.setTags(new HashSet<Tag>() {{ add(new Tag("Foo")); add(new Tag("Bar")); }});
Binder<Person> binder = new Binder<>(Person.class);
TextField name = new TextField();
binder.bind(name, Person::getName, Person::setName);
CheckBoxGroup<Tag> tags = new CheckBoxGroup<>();
tags.setItems(new HashSet<Tag>() {{ add(new Tag("Foo")); add(new Tag("Bar")); add(new Tag("Test")); }});
binder.bind(tags, Person::getTags, Person::setTags);
this.setContent(new VerticalLayout(new Label("Hello Vaadin!"), name, tags));
binder.setBean(person);
}
}
The expected behavior for me would be, that the CheckBoxes Foo and Bar are preselected, when the UI is rendered:
But the actual result is that none of the CheckBoxes are preselected:
When I debug my code I can see that updateSelection of Class AbstractMultiSelect is called with [Bar, Foo] as addedItems, but unfortunatly this update is not populated to the UI.
I think I'm missing something here, but I'm note sure what it could be. Do I need to implement a custom Converter or some kind of Comparator to tell Vaadin that new Tag("Foo") is equal to new Tag("Foo")?
Thanks in advance.
Answering my own question (-.-):
The class Tag needs to overrid the method equals. Here is the complete Class:
package com.vaadin.test;
public class Tag {
private String name;
public Tag(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return name;
}
@Override
public boolean equals(Object obj) {
return ((Tag) obj).getName().equals(this.getName());
}
}