I have two ComboBoxes in my editable Grid where the second ComboBox based on the first one. So for example you would have Car Make and then Car Model. When you change the make combobox the model combobox then changes accordingly.
With that in mind I have:
ComboBox<String> makeComboBox = new ComboBox<String>();
ComboBox<String> modelComboBox = new ComboBox<String>();
Specifically:
grid.addColumn(CarRental::getPerson)
.setEditorBinding(binder.forField(personComboxBox).bind(CarRental::getPerson, CarRental::setPerson));
grid.addColumn(CarRental::getMake)
.setEditorBinding(binder.forField(makeComboxBox).bind(Car::getMake, Car::setMake));
grid.addColumn(CarRental::getModel)
.setEditorBinding(binder.forField(modelComboxBox).bind(Car::getModel, Car::setModel));
The key here is that I want the modelComboBox to change if the makeComboBox is changed. In other words if you select Honda then I want the model ComboBox to change to Fit, Civic, Accord, and so on. To do this I add a SelectionListener (but it could also be a valueChangeListener, it doesn’t matter, the effect is still the same).
Specifcally I have:
makeComboBox.addSelectionListener(event ->
{
modelComboBox.clear();
modelComboBox.setItems(getModelsBasedOnMake(makeComboBox.getValue()));
// Assuming someone has just edited the make value,
// say changed from Toyota to Honda, then I want the model selected to be empty
});
Because the ComboBox can be different I’ve added some logic to update the components on the OpenListener for the Grid Editor. Specifically I do:
grid.getEditor().addOpenListener(open ->
{
...
CarRental selectedCarRental = (CarRental)event.getBean();
makeComboBox.setItems(makeList);
modelComboBox.setItems(getModelsBasedOnMake(selectedCarRental.getMake()));
});
The problem here is that the modelComBoxbox tends to be unselected because if you look at it there’s no guarantee which value it will be because there is a conflict.
I looked at temporarily disabling the selectionListener but this all the remove listeners have been deprecated with Vaadin 8. Therefore how can I setup the grid to be able to edit both the car make and model in the grid?