Possible bug and/or odd ComboBox behavior

I’ve got a combobox of Person such as:

ComboBox personComboBox = new ComboBox<>(“People”, List.of(
new Person(“John”, “Smith”),
new Person(“Jane”, “Doe”)));

where Person’s toString just returns the first name. That is return person.firstname.

I then have add two buttons:

add(new Button(“Show”, click → {

add(new Button(“Select”, click → {
personComboBox.setValue(new Person(“John”, “Smith ----- wrong”));

If I select John smith from the list and then click the Show button all is good. However if I click on the Select button and then the Show button it will show the last name as “Smith ----- wrong”. If I then reselect with the mouse John Smith it comes back as good. If I again click on the Select button the wrong instance is again shown. In other words it seems like the ComboBox replaces the instance when using the setValue.

I know there is an issue between using the toString() and equals() or hashCode() but shouldn’t the combobox use the existing instance rather than replace it with a new one? This is bad for me because the replacement is incomplete. Also it means that any reliance on any comparison later could be compromised.

In my case I’m using a fuller Person object in the combobox (for rendering) and say a partial person in the main grid item list. So for example it could be the doctor for a patient list where the patient instance only has minimal data to save on loading the patient grid list but when editing the grid I want a full doctor’s information for the combobox (above name I may want speciality, etc. so that it can be rendered in the combobox)

Any suggestions or tips on how to select an item in the combobox so that it selects the instance in the combobox rather than replace it?

You have two different problems; hash code / equals are the most important parts and combo box, grid and all other list based components operate on it, therefore those have to be unique.

The second problem about “combo box selects another value when two times have the same label” is reported here and hopefully soonish ™ fixed https://github.com/vaadin/web-components/issues/6587

The only thing is that I only have one item with John Smith…

The “Smith ----- wrong” is never an item within the combobox, I only ever use it in setValue().

Yeah that can’t work, it has to be in the list

Or custom value is allowed (but I think that only allows custom values from the client, not server)

I think because it’s matching the label (toString()) that it’s allowing it. However it’s also replacing it which is odd…

Similar equals/hash code returns? But too tedious to debug; setValue(unknown value to the list) is just not supported :sweat_smile:

But the thing is that it’s actually selecting a value (setting a value).

and replacing the internal instance on getValue() right after.

ComboBox personComboBox = new ComboBox<>(“People”, List.of(
new Person(“John”, “Smith”),
new Person(“Jane”, “Doe”)));

personComboBox.setValue(new Person(“John”, “Smith ----- wrong”));

Works. You can then in the code do:

personComboBox.getValue() and the instance returned is the instance from setValue() and not one from the internal list.

assuming toString is just return firstname;

And if in the UI I select John Smith my mouse and do getValue() it returns the correct instance.

Just stating the obvious: you doing something unsupported where nobody even tested what happens :sweat_smile:


Ok I misunderstood then.