javavaadinmulti-select

method select for MultiSelectComboBox not working


I have a MultiSelectComboBox but when trying to select some items through its select() method nothing happens.

My code looks as follows:

Binder<Technology> binder = new BeanValidationBinder<>(Technology.class);
MultiSelectComboBox<TechnologyLabel> multiComboBox = new MultiSelectComboBox<>("Labels");

binder
                .forField(this.multiComboBox)
                .bind(Technology::getLabels, Technology::setLabels);

        List<TechnologyLabel> labelList = technologyLayout.technologyLabelService.getTechnologyLabels(technology);
        List<Label> containedLabels = new ArrayList<>();
        for (var tl : labelList) {
            containedLabels.add(tl.getLabel());
        }
        List<Label> labels = labelService.findAllLabels();

        for (var label : labels) {
            if (!containedLabels.contains(label)) {
                labelList.add(new TechnologyLabel(technology, label));
            }
        }
        multiComboBox.setItems(labelList);
        multiComboBox.setItemLabelGenerator(TechnologyLabel::getLabelName);
        note.setMaxHeight("10em");

        multiComboBox.select(labelList.get(0);

What I do above is querying my ManyToMany relation to find all labels given the technology. Then I find all the labels and create a list from that. Lastly, I try to select first item of the list, but it does not show a tick next to it.


Solution

  • Using @cfrick as answer basically and all credits go to him, but i wanted to leave a proper answer. I had the same problem, i defined the equals and hashcode method in my TechnologyLabel and it started working. i only had to compare by id in my code.

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null || getClass() != obj.getClass()) {
            return false;
        }
        TechnologyLabel technologyLabel = (TechnologyLabel) obj;
        return Objects.equals(id, technologyLabel.id);
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(id);
    }