JPAContainer and ComboBox revisited

I have a problem very similar to that discussed in
https://vaadin.com/forum/#!/thread/2729098
and
http://blog.oio.de/2014/04/25/select-nested-javabeans-vaadin-fieldgroup/

An editor dialog for a “Component” object, which refers to an “Application” –

Containers:

private JPAContainer<Application> myapplications; private JPAContainer<Component> mycomponents; Form:

[code]
class CompForm extends FormLayout {
@PropertyId(“application”)
ComboBox application;

    public CompForm() {
        setSpacing(true);

        application = new ComboBox("Application", myapplications);
        application.setConverter(new SingleSelectConverter<Application>(application));
        application.setImmediate(true);
        addComponent(application);

        this.setImmediate(true);
    }
}

[/code]And then use it with a field group:

[code]
editorForm = new CompForm();

    fieldGroup = new FieldGroup(componentItem);
    fieldGroup.setBuffered(true);
    fieldGroup.bindMemberFields(editorForm);

[/code]Everything works – I have the elements of the JPAContainer in the ComboBox, and if one is selected, the write to the database is done correctly.

However, the values displayed in the combobox are the database’s ApplicationId integer values. Based on what I had read so far, using SingleSelectConverter() should cause the toString() representation from the Application bean to be displayed instead. What am I doing wrong here?

Thanks.

The default behavior of a ComboBox is to use the toString() method of the items’ ID property for the captions. You can use the following method (inherited from the superclass AbstractSelect) to change that behavior.

setItemCaptionMode(AbstractSelect.ItemCaptionMode mode)

Thanks, this works, if I set mode=ITEM instead of ID.

I also found a workaround using FieldFactory:

FieldFactory fieldFactory = new FieldFactory(); Field application = fieldFactory.createField(mycomponents, componentItem, "application", this); addComponent(application) But, I can’t use the @PropertyId annotation on the field this way, so that is a disadvantage. Also, the selector it gives me is a NativeSelect, and I would prefer a ComboBox.

So, thanks again for the fix.

I have not used an annotation in that fashion. However, in terms of your example, I would set the ComboBox choice captions using the “application” property in the following fashion. If I recall correctly, these methods need to be called in the order shown.

application.setItemCaptionPropertyId("application");
application.setItemCaptionMode(AbstractSelect.ItemCaptionMode.PROPERTY);