Thats a good question! After some investigation I found the following solution:
You have to create your custom FieldGroupFieldFactory (why see below):
binder.setFieldFactory(new DefaultFieldGroupFieldFactory() {
@Override
public <T extends Field> T createField(Class<?> type, Class<T> fieldType) {
if (type.isAssignableFrom(String.class) && fieldType.isAssignableFrom(ComboBox.class)) {
return (T) new ComboBox();
}
return super.createField(type, fieldType);
}
});
The reason why:
If you have a look at the vaadin source code of DefaultFieldGroupFieldFactory.java you will see that in the end a ComboBox
will only be created IF you provide an Enum as "property data source". You provide a String so the DefaultFieldGroupFieldFactory
wants to create a TextField
. But you provided a ComboBox
. In the end there is an Exception thrown.
With your own Factory it will work.
Don't forget to setItemDataSource(bean)
and commit()
your binder to actually write the gender to the bean.