gpt4 book ai didi

java - Vaadin:将枚举值绑定(bind)到组合框(ConversionException)

转载 作者:行者123 更新时间:2023-11-30 06:52:14 30 4
gpt4 key购买 nike

我想将模型中的枚举值绑定(bind)到 ComboBox。我尝试了什么:

型号:

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;
}

表格:

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(); // This is what causes the error
}
}

当我调用 commit() 时出现此错误:

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

有什么方法可以将我的模型的枚举绑定(bind)到 ComboBox 吗?

感谢您的帮助!

更新(André Schild 回答):

我想显示枚举值的字符串表示,所以我想我需要使用字符串而不是整数?我发现“StringToEnumConverter”中实际上有一个构建。但是,我尝试使用 StringToEnumConverter 和我自己的“StringToStatusConverter”:

cStatus.setConverter((Converter) new StringToEnumConverter());
cStatus.addItems(EnumSet.allOf(Status.class));`
// cStatus.addItem(Status.CANCELLED); // Did not work
// cStatus.setConvertedValue(Status.EOL); // Did not work

现在我得到:

java.lang.ClassCastException: xx.xxx.xxx.entities.modeldefdef.Model$Status cannot be cast to java.lang.String
at com.vaadin.data.util.converter.StringToEnumConverter.convertToModel(StringToEnumConverter.java:32) ~[vaadin-server-7.5.6.jar:7.5.6]

...对于两个转换器。

最佳答案

在您的 buildContent() 方法中写入:

// ...
final BeanItemContainer<Status> container = new BeanItemContainer<>(Status.class);
container.addAll(EnumSet.allOf(Status.class));
cStatus.setContainerDataSource(container);
cStatus.setItemCaptionPropertyId("caption");
basicContent.addComponent(cStatus);
// ...

为什么?您的 ComboBox 被 FieldGroup 绑定(bind)到 Status 类型的属性。但是您当前的 Container BeanContainer 具有 Integer 类型,即容器 ID 的类型。修复容器的 ID 类型必须与属性的类型相匹配的问题。这是通过使用 BeanItemContainer 完成的,它使用 Bean 类型作为对象 ID。 Here问题得到了更详细的解释。

关于java - Vaadin:将枚举值绑定(bind)到组合框(ConversionException),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39365405/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com