显示属性项名称而不是ID

时间:2017-02-03 17:58:52

标签: java combobox vaadin7

我有多个组件的布局(4个textFields和2个组合框)。 这是如何定义的:

private ManufacturerRepository manRepo;
InteriorDoor interiorDoor;

Manufacturer manufacturer;

ComponentHelper componentHelper;

@PropertyId("width")
private TextField widthField;

@PropertyId("height")
private TextField heightField;

@PropertyId("thickness")
private TextField thicknessField;

@PropertyId("modelName")
private TextField modelName;

@PropertyId("hasGlass")
private ComboBox glassCombo;

@PropertyId("manufacturerId")
private ComboBox manCombo;

private BeanItemContainer<Manufacturer> beanItemContainer;

beanItemContainer用于从数据库中获取数据:

beanItemContainer = new BeanItemContainer<>(Manufacturer.class, manRepo.findAll());
manCombo.setContainerDataSource(beanItemContainer);

不介意glassCombo,因为它具有常量数据,并且绑定在那里很有效。

这是我绑定字段的方式:

// binding data to fields
    BeanFieldGroup binder = new BeanFieldGroup<>(InteriorDoor.class);
    binder.setItemDataSource(interiorDoor);
    binder.setBuffered(false);
    binder.bindMemberFields(this);

到目前为止一切都那么好,但是当布线字段发生时,问题就出现了manCombo - 从组合框中选择项目时,我收到了这个

  

引起:com.vaadin.data.util.converter.Converter $ ConversionException:无法将值转换为Long       在com.vaadin.ui.AbstractField.convertToModel(AbstractField.java:778)〜[vaadin-server-7.7.5.jar:7.7.5]       在com.vaadin.ui.AbstractField.convertToModel(AbstractField.java:755)〜[vaadin-server-7.7.5.jar:7.7.5]       在com.vaadin.ui.AbstractField.setValue(AbstractField.java:539)〜[vaadin-server-7.7.5.jar:7.7.5]       ...省略了49个常用帧   引起:com.vaadin.data.util.converter.Converter $ ConversionException:无法将类型.domain.Manufacturer的值转换为模型类型类java.lang.Long。没有设置转换器且类型不兼容。       在com.vaadin.data.util.converter.ConverterUtil.convertToModel(ConverterUtil.java:182)〜[vaadin-server-7.7.5.jar:7.7.5]       在com.vaadin.ui.AbstractField.convertToModel(AbstractField.java:775)〜[vaadin-server-7.7.5.jar:7.7.5]       ...省略了51个常见帧

请帮忙

P.S我试图施放为(((Manufacturer).getValue).getId(),但它没有效果

更新1

@Marco C解决方案不起作用。现在,在调用布局时,我会收到:

  

java.lang.NullPointerException:null at   java.lang.Class.isAssignableFrom(Native Method)〜[na:1.8.0_121] at   com.vaadin.data.util.converter.ConverterUtil.canConverterPossiblyHandle(ConverterUtil.java:243)   〜[vaadin-server-7.7.5.jar:7.7.5] at   com.vaadin.ui.AbstractField.setPropertyDataSource(AbstractField.java:649)   〜[vaadin-server-7.7.5.jar:7.7.5] at   com.vaadin.data.fieldgroup.FieldGroup.bind(FieldGroup.java:272)   〜[vaadin-server-7.7.5.jar:7.7.5] at   com.vaadin.data.fieldgroup.BeanFieldGroup.bind(BeanFieldGroup.java:155)   〜[vaadin-server-7.7.5.jar:7.7.5] at   com.vaadin.data.fieldgroup.FieldGroup.buildAndBindMemberFields(FieldGroup.java:1011)   〜[vaadin-server-7.7.5.jar:7.7.5] at   com.vaadin.data.fieldgroup.FieldGroup.bindMemberFields(FieldGroup.java:856)   〜[vaadin-server-7.7.5.jar:7.7.5] at   com.reborn.doorshop.ui.components.InteriorEditor.editEntity(InteriorEditor.java:156)   〜[classes /:na] at   com.reborn.doorshop.ui.views.InteriorView.lambda $ $的initComponents $ 61446b05 1(InteriorView.java:53)   〜[classes /:na] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native   方法)〜[na:1.8.0_121] at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

UPDATE2:

manCombo.setConverter(new Converter<Object, Long>() {
        @Override
        public Long convertToModel(Object value, Class<? extends Long> targetType, Locale locale)
                throws ConversionException {
            if (value == null) {
                return null;
            }
            return ((Manufacturer)value).getId();
        }

        @Override
        public Object convertToPresentation(Long value, Class<?> targetType, Locale locale)
                throws ConversionException {
            if (value == null) {
                return null;
            }
            return beanItemContainer.getItemIds().stream().filter( m -> value.equals(m.getId()))
                    .findFirst().orElse(null);
        }

        @Override
        public Class<Long> getModelType() {
            return null;
        }

        @Override
        public Class<Object> getPresentationType() {
            return null;
        }
    });

1 个答案:

答案 0 :(得分:1)

您可以实施转换器制造商 - &gt;长并将其设置在组合框上

$user->active_profile