Important Notice - Forums is archived
To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.
Bind Enum values to ComboBox
I want to bind enum values from my Model to a ComboBox. What I tried:
Model:
public class Model{
public enum Status {
Neu, Analyse, Pruefung, Freigabe, OnTheRoad, EOL, CANCELLED;
public String getCaption() {
return name();
}
public int getId() {
return ordinal();
}
}
private Status status;
}
Form:
public class MyForm extends CustomComponent{
private final BeanFieldGroup<Model> binder;
@PropertyId("status")
private final ComboBox cStatus = new ComboBox("Status");
public MyForm (Model model) {
final HorizontalLayout content = new HorizontalLayout();
content.addComponent(buildContent());
// Now use a binder to bind the members
binder = new BeanFieldGroup<>(Model.class);
// We need an item data source before we create the fields to be able to
// find the properties, otherwise we have to specify them by hand
final BeanItem bean = new BeanItem<>(model);
binder.setItemDataSource(bean);
binder.buildAndBindMemberFields(this);
setCompositionRoot(content);
}
private buildContent(){
final FormLayout basicContent = new FormLayout();
final BeanContainer<Integer, Status> container = new BeanContainer<>(Status.class);
container.setBeanIdProperty("id");
container.addAll(EnumSet.allOf(Status.class));
cStatus.setContainerDataSource(container);
cStatus.setItemCaptionPropertyId("caption");
basicContent.addComponent(cStatus);
return basicContent;
}
public void commit() throws CommitException {
binder.commit();
}
}
When i call commit() I'm getting this error:
Caused by: com.vaadin.data.util.converter.Converter$ConversionException: Unable to convert value of type java.lang.Integer to model type class com.xx.test.entities.def.Model$Status. No converter is set and the types are not compatible.
at com.vaadin.data.util.converter.ConverterUtil.convertToModel(ConverterUtil.java:181) ~[vaadin-server-7.5.6.jar:7.5.6]
at com.vaadin.ui.AbstractField.convertToModel(AbstractField.java:751) ~[vaadin-server-7.5.6.jar:7.5.6]
at com.vaadin.ui.AbstractField.convertToModel(AbstractField.java:731) ~[vaadin-server-7.5.6.jar:7.5.6]
at com.vaadin.ui.AbstractField.getConvertedValue(AbstractField.java:817) ~[vaadin-server-7.5.6.jar:7.5.6]
at com.vaadin.ui.AbstractField.commit(AbstractField.java:253) ~[vaadin-server-7.5.6.jar:7.5.6]
at com.vaadin.data.fieldgroup.FieldGroup.commitFields(FieldGroup.java:509) ~[vaadin-server-7.5.6.jar:7.5.6]
at com.vaadin.data.fieldgroup.FieldGroup.commit(FieldGroup.java:481) ~[vaadin-server-7.5.6.jar:7.5.6]
... 49 more
Is there any way to bind the Enum of my Model to the ComboBox ?
Thanks for any help!
Hello,
You could try to add from integer to Model.Status -converter to the combobox. You could use the ordinal from the enum to do the conversion.
-Matti
You could try the EnumSelect from the Viritin addon. It contains a lot of additional fields like this.
EnumSelect<MyEnum> type = new EnumSelect<MyEnum>("Type");
I used BeanItemContainer.
// ...
final BeanItemContainer<Status> container = new BeanItemContainer<>(Status.class);
container.addAll(EnumSet.allOf(Status.class));
cStatus.setContainerDataSource(container);
cStatus.setItemCaptionPropertyId("caption");
basicContent.addComponent(cStatus);
// ...
What is BeanItemContainer? is it your custom component?