ComboBox Query ?

Hello,
I am using Vaadin 14.
I want to display a value in frontend using ComboBox but the value I want at backend should be different.
Example -
If I select “United States of America” from comboBox then at the backend or at bean the value should be set as “USA” i.e. I basically want a Key, Value pair in ComboBox.
Also when I retrieve the key from backend i.e. “USA” the value in comboBox should be “United States of America”.

You can use the [setItemLabelGenerator]
(https://vaadin.com/api/platform/14.1.20/com/vaadin/flow/component/combobox/ComboBox.html#setItemLabelGenerator-com.vaadin.flow.component.ItemLabelGenerator-) to control how a value is displayed in the ComboBox. The value returned from the ItemLabelGenerator will be displayed to the user (e.g. “United States of America”), but in the backend it will remain what you set (e.g “USA”).

Thank You, For your valuable response, I have one more query.

Can I return a collection from apply() method of ItemLabelGenerator. I am using ItemLabelGenerator but I am facing problems with that, as there are many key values pairs so I kept them all in one HashMap, Now how can I return a particular label from ItemLabelGenerator for particular key So that key will get bind to my bean property.

@Tulio Garcia

You can not return a Collection. Would something like this work for you?

        final Map<String,String> itemLabels = new HashMap<>();
        itemLabels.put("USA","United States of America");
        final ComboBox<String> comboBox = new ComboBox<>();
        comboBox.setItems("USA");
        comboBox.setItemLabelGenerator(itemLabels::get);
        add(comboBox);

If you are using binder, you could have something like this

        final Binder<Country> binder = new Binder<>();
        binder.forField(comboBox).bind(Country::getCode,Country::setCode);
		
		class Country {
        private String code;

        public String getCode() {
            return code;
        }

        public void setCode(String code) {
            this.code = code;
        }
    }
		

Thank You so much,
Its working Now, I am able to get key and show the value as label.

		final Map<String,String> itemLabels = new HashMap<>();
        itemLabels.put("USA","United States of America");
        itemLabels.put("UK","United Kingdom");
        itemLabels.put("IN","India");
        
        final ComboBox<String> comboBox = new ComboBox<>("Country");
        
        comboBox.setItems(itemLabels.keySet());
        comboBox.setItemLabelGenerator(itemLabels::get);
        
        comboBox.addValueChangeListener(evt -> {
        	System.out.println(comboBox.getValue());
        });
        
        add(comboBox);

@Tulio Garcia

Can we do the same with the CheckBox. Display the value and send the key to backend.

@Tulio Garcia

You can use CheckboxGroup. It also has a [setItemLabelGenerator]
(https://vaadin.com/api/platform/14.1.20/com/vaadin/flow/component/checkbox/CheckboxGroup.html#setItemLabelGenerator-com.vaadin.flow.component.ItemLabelGenerator-)