In my application I want to display an object’s properties (of type let’s say: “MyObject”) and make them editable using a form.
One of the properties is a Boolean which I want to display and make changeable using a ComboBox like so:
public class MyForm extends FormLayout {
...
ComboBox<Boolean> enabled = new ComboBox<>("enabled"); //$NON-NLS-1$
...
Binder<Node> binder = new BeanValidationBinder<>(MyObject.class); // a Binder that is aware of bean validation annotations
public MyForm() {
enabled.setItems(Boolean.TRUE, Boolean.FALSE);
}
I then later initialize the object:
MyObject myObj = new MyObject();
... setting object attributes ...
and bind the form with the object using:
this.binder.readBean(myObj); // to bind the values from the object to the UI fields.
For some reason which I don’t understand this yields the following exception:
java.lang.IllegalStateException: Cannot set a value for a ComboBox without items. Use setItems to populate items into the ComboBox before setting a value.
at com.vaadin.flow.component.combobox.ComboBox.setValue(ComboBox.java:372)
at com.vaadin.flow.data.binder.Binder$BindingImpl.convertAndSetFieldValue(Binder.java:1275)
at com.vaadin.flow.data.binder.Binder$BindingImpl.initFieldValue(Binder.java:1196)
at com.vaadin.flow.data.binder.Binder$BindingImpl.access$200(Binder.java:1032)
at com.vaadin.flow.data.binder.Binder.lambda$readBean$2(Binder.java:1821)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1540)
at com.vaadin.flow.data.binder.Binder.readBean(Binder.java:1813)
...
While I understand the error message I can’t see how this can possibly be true.
I am 100% positive (I added tons of denbug statements into my code by now) that I populate that darn ComboBox with values BEFORE calling that readBean(…) method. So how and why can this then claim that the ComboBox is in a wrong state and that I need to call setItems() first?
I even tried with the constructor
ComboBox<Boolean> enabled = new ComboBox<>("enabled", Boolean.TRUE, Boolean.FALSE); //$NON-NLS-1$
which makes it absolutely sure that there are values but got the very same error.
There must something else be missing or wrong here! Some other assumption or precondition for ComboBox that is not fulfilled here.
Does the form and hence the fields have to be visible before I call that binder or what else could possibly be wrong here and cause this IllegalStateException?