combobox goes two times to listener

There are still three small things to solve with comboboxes.

  1. Application goes two times to ValueChangeListener when an item is selected in a combobox whose items have been removed and then populated again.
    Combos are created: for (int i = 0; i < comboId.length; i++) { comboItems = dataConnection.populateCombo(comboId[i] ); combos[i] = new ComboBox(comboId[i] ); combos[i] .addListener(listener); for (String s : comboItems) { combos[i] .addItem(s); } combos[i] .setImmediate(true); formLayout.addComponent(combos[i] ); }
    When a new item is selected in car (combos[0]
    ) it removes old items in malli (combos[1]
    ) and adds new items:
combos[1]
.removeAllItems();
...
for (String s : comboItems) {
	System.out.print(s + ", ");
	combos[1]
.addItem(s);
}
  1. The choosen item in combos[1]
    or malli does not stay visible but listener works.
  2. When a combobox is populated Vaadin adds an empty item at the beginning in the list of items.
    Point:
    When the first empty item in combos[1]
    or malli is selected it does not find listener.

In all other combos that has not been changed since populating the first empty item works well.

Extra question:
To solve point 1 and 2 makes code more fluent and might help with point 3?

Tapani

Good morning Tapani!

I gather from your post that you have the same ValueChangeListener attached to both comboboxes? In this case the ValueChange is triggered first for the “car” combobox and when the code in the listener refills the “malli” combobox the ValueChange will be triggered again.

Since the value of the “malli” combobox will be null the first time (unless something else is selected), the ValueChangeListener will be called with a null value.

If you don’t want it to be possible to select null values, call setNullItemsAllowed(false) on the combobox.

Hope I understood your problem and this helps,
/Jonatan

Jonatan,
two different ValueChangeListeners sounds better for me. It makes code so clean.

Thanks,
Tapani