gatanaso/multiselect-combo-box-flow

SetValue does not check the items in the list view

gianluca-valentini opened this issue · 5 comments

Hi,
I'm using last vaadin 14 LTS and v.2.5.0 multiselect version.
It works fine but when I use the setValue, the component set the item correctly in the textfield but does not check it in the list
Look this image
image
I use a object render to show the item list.

I tried the 3.0.2 version but I have the same result
The problem is that I can add the same item twice

Same problem for me, is there a workaround ?
How the component check the equality on an object ?

In my case solved overriding the hashCode() Method of my Object Class
@Override public int hashCode() { return key.hashCode(); }

Hi @ado2000
can you explain it with an example?

The component uses the hashCode to check the equality of one in the items respect the one set as values

So, you have to override the hashCode method in order to enable the component to select the proper object in the drop down list.

It depend on how you consider two objects equals.
In my case, my object is quite easy, I have a key an a value object, for me, if the key is the same, the object is the same.

public class KeyValueCombo {
    private Object key;
    private Object value;

    public KeyValueCombo(Object key, Object value) {
        this.key = key;
        this.value = value;
    }

   
    public Object getKey() {
        return key;
    }

    public void setKey(Object key) {
        this.key = key;
    }

    public Object getValue() {
        return value;
    }

    public void setValue(Object value) {
        this.value = value;
    }

    @Override
    public boolean equals(Object obj) {

        if(obj == null) return false;

        return ((KeyValueCombo)obj).getKey().equals(getKey());
    }

    @Override
    public int hashCode() {
        return key.hashCode();
    }
}

It's clear.
Thanks a lot