Vaadin 12 ItemLabelGenerator of ComboBox when used in grid ComponentRenderer

854 views Asked by At

Today I have upgraded from Vaadin 11.0.2 to 12.0.0 - Everything went smooth, except one thing:

In a grid of mine where I have a rendered column to show a ComboBox, there is a strange issue with the ItemLabelGenerator. I defined it as follows:

grid.addColumn(new ComponentRenderer<>(gridItem -> {

    ComboBox<MyObject> comboBox = new ComboBox<>();
    comboBox.setItems(myObjectsService.findAll());
    comboBox.setValue(gridItem.getMyObject());
    comboBox.setItemLabelGenerator(MyObject::getName); // MyObject::getName returns String
    // comboBox.addValueChangeListener omitted
    return comboBox;

}))
    .setHeader("MyObject")
    .setId("myObject");

This has been working fine in Vaadin 11.0.2 but now the item-labels are displayed as package.path.to.myobject.MyObject@41d8d522 and not the actual name of gridItem.getMyObject();
When I click on the ComboBox to show all options, the labels are correct! but as soon as I select one, it turns into the aforementioned wrong string.

Important detail: for testing reasons I have now added a similar ComboBox with the same setup into a simple VerticalLayout (AKA not in a grid), and there everything works perfectly fine. That is why I think the issue is with the ComponentRenderer somehow and not with the ComboBox alone.

Is this a bug, or have I missed something when upgrading to 12.0.0 ?

In the vaadin blog post about the new release of Vaadin 12, I see that there is one known breaking change, and it has to do with ComboBox:

If you are coming from Vaadin 10 or 11, you should update the platform dependency in your project build file. The only breaking change we introduced was because ComboBox now supports server-side lazy-loading. If you are using filtering with a ComboBox see instructions on fixing the possible compilation issue.

However, no filtering whatsoever is involved in my case.

1

There are 1 answers

1
kscherrer On BEST ANSWER

This answer was written by Diego Sanz Villafruela in the Vaadin Forum, where I raised this exact issue too.

I created an example similar to yours and I discover that the order in which you set the value and the ItemLabelGenerator matters.

You should put comboBox.setValue after setting the comboBox.setItemLabelGenerator.

Otherwise the method String.valueOf(obj) will be called the first time, giving you the object's representation (MyObject@41d8d522) and not the name.