Vaadin 8 Combobox: I need to display one value and set another

It’s possible to display on value made with setItemCaptionGenerator but set another ?

Thanks
Fernando

If I understand your question correctly, yes, the underlying value doesn’t need to be the same as the caption on the ComboBox.

-Olli

Hi Olli, let met reformulate the question, i need change the caption when the user do click in a item in the drop-down list
Maybe, there is a Event where i can do that .

Thanks
Fernando

Hi,

I tried it out and this was the best that I could come up with:

    ComboBox<String> comboBox = new ComboBox<>();

    ItemCaptionGenerator<String> icg = new ItemCaptionGenerator<String>() {
        @Override
        public String apply(String item) {
            if (!comboBox.getSelectedItem().isPresent()) {
                return "not selected: " + item;
            }
            return comboBox.getSelectedItem().get().equals(item) ? "selected: " + item : "not selected: " + item;
        }
    };

    @Override
    protected void init(VaadinRequest vaadinRequest) {
        final VerticalLayout layout = new VerticalLayout();

        List<String> items = new ArrayList<>();
        items.add("one");
        items.add("two");
        items.add("three");

        comboBox.setItems(items);
        comboBox.setEmptySelectionAllowed(false);

        comboBox.setItemCaptionGenerator(icg);

        comboBox.addValueChangeListener(event -> comboBox.setItemCaptionGenerator(icg));

        layout.addComponent(comboBox);

        setContent(layout);
    }

Hope this helps,

-Olli

Yes Olli, Excellent, that is was i needed!

Thanks.
Fernando

You’re welcome!

-Olli