Vaadin 8 ComboBox and empty selection

when trying to use a ComboBox, I tried to set the empty selection caption with the followings

setEmptySelectionAllowed(true);
setEmptySelectionCaption(“My empty caption”);

The empty caption appears well in the Combobox menu, but once selected, the selected value is always blank, whatever I did. And setting it programmtically with setValue (null) does not make it appear

I check the setItemCaptionGenerator, but my lambda expression is not called with a null value.

Did I completly miss something, or is there a bug ? I’m using 8.0.5

Thanks in advance

Hi Eric,

I also have tried to set EmptySelection as you did and some other ways, but result was the same- the caption is not shown in the selectedField.
I have found a “workaround”, but in this case you have to set null, but you can manapulate with it on server side the way you want.
Anyway,

[code]
List planets = new ArrayList<>();
planets.add(“Mercury”);
planets.add(“Venus”);
planets.add(“Earth”);
planets.add(null);

    ComboBox<String> select = new ComboBox<>("Select or Add a Planet");
    select.setEmptySelectionAllowed(false);
    select.setItems(planets);
    select.setItemCaptionGenerator( c-> c==null? "EMPTY VALUE" : String.valueOf(c));
    select.setValue(null);
    Button a = new Button("choose");
    a.addClickListener(event -> {
   //This prints item itself;it could be Optional.empty, if it was null or Optional[Mercury]

        System.out.println(select.getSelectedItem());
    });
    select.addSelectionListener(event -> {

        //This prints caption. "EMPTY VALUE" or Mercury
        System.out.println(select.getValue());
    });

[/code]You will get the result you want, but I suggest you to create a bugticket to https://github.com/vaadin/framework/issues if you will so.

Hopefully it helps,
Best regards,
Anastasia

Thanks for trying. I did enter a bug

https://github.com/vaadin/framework/issues/9079

Best regards

Eric

Is this bug already fixed in Vaadin 8.1.6? I’m asking as I struggle with a similar behaviour. I’m using the NativeSelect component and in fact I can select the emptySelectionCaption and it also gets displayed. My problem is, that the object does not display the emptySelectionCaption when it is initialized with an emptyValue, neither if the null value comes from the databinding nor if I set it manually by nativeSelect.setValue(null)

Any ideas why the emptySelectionCaption is not display initially?

I’m guessing that that’s another instance of the same bug. It was fixed for ComboBox, but still remains in NativeSelect. Can you create a ticket with a small code sample at
https://github.com/vaadin/framework/issues/
? You can reference https://github.com/vaadin/framework/issues/9079 there.

-Olli