ComboBox with Enum help

I have been testing this all day and can’t seem to move any further along so I am asking for Vaadin gurus to help me find a soluion.

My issue centers around using enums with a combobox. I have searched the forums for pointers and found some really good tips. I would appreciate all the help. The (2) problems I am having are:

  1. The bean enum value does not displayed or not selected when using the combobox setValue method.
  2. When saving the bean using FieldGroup commit(), I get the following error.

Caused by: com.vaadin.data.util.converter.Converter$ConversionException: Could not convert value to MFContactType at com.vaadin.ui.AbstractField.convertToModel(AbstractField.java:754) at com.vaadin.ui.AbstractField.convertToModel(AbstractField.java:731) at com.vaadin.ui.AbstractField.getConvertedValue(AbstractField.java:817) at com.vaadin.ui.AbstractField.commit(AbstractField.java:253) ... 49 more Caused by: com.vaadin.data.util.converter.Converter$ConversionException: Unable to convert value of type java.lang.Integer to model type class aaptimize.enumType.MFContactType. No converter is set and the types are not compatible. at com.vaadin.data.util.converter.ConverterUtil.convertToModel(ConverterUtil.java:181) at com.vaadin.ui.AbstractField.convertToModel(AbstractField.java:751) ContactForm

[code]
package aaptimize.account;

import java.util.EnumSet;

import javax.persistence.EntityManager;

import aaptimize.domain.EntityAccountContact;
import aaptimize.enumType.MFContactType;
import aaptimize.uidesign.account.ContactFormDesign;

import com.vaadin.data.fieldgroup.FieldGroup;
import com.vaadin.data.util.BeanContainer;
import com.vaadin.data.util.BeanItem;
import com.vaadin.data.util.converter.StringToIntegerConverter;

@SuppressWarnings(“serial”)
public class ContactForm extends ContactFormDesign {

protected EntityManager entityManager;
protected BeanItem<EntityAccountContact> contactBean;
protected FieldGroup binder;
private boolean newContact = false;

public ContactForm(EntityManager entityManager) {
    this.entityManager = entityManager;
    this.contactBean = new BeanItem<EntityAccountContact>(
            new EntityAccountContact());
    this.binder = new FieldGroup(this.contactBean);
    this.newContact = true;

    this.initComponents();
}

public ContactForm(EntityManager entityManager,
        BeanItem<EntityAccountContact> contactBean) {
    this.entityManager = entityManager;
    this.contactBean = contactBean;
    this.binder = new FieldGroup(this.contactBean);

    this.initComponents();
}

private void initComponents() {
    this._contactType.setInvalidAllowed(false);
    this._contactType.setNullSelectionAllowed(false);
    this._contactType.setInputPrompt("Select...");
    this._contactType.setItemCaptionPropertyId("caption");
    this._contactType.setImmediate(true);
    this._contactType.setConvertedValue(new StringToIntegerConverter());
    this._contactType.setContainerDataSource(this.getEnumDataSource());
    
    this.binder.bind(_name, "contactName");
    this.binder.bind(_email, "emailAddress");
    this.binder.bind(_phoneField, "telephoneNumber");
    this.binder.bind(_extField, "phoneExtension");
    this.binder.bind(_fax, "faxNumber");
    this.binder.bind(_contactType, "contactType");
    
    if (!this.newContact) {
        [color=#FF0000]

this._contactType.setValue(contactBean.getBean());
[/color]
}


this._saveButton.addClickListener(listener → {
try {
binder.commit();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
});

}

private BeanContainer<String, MFContactType> getEnumDataSource() {
    final BeanContainer<String, MFContactType> container = new BeanContainer<>(
            MFContactType.class);
    container.setBeanIdProperty("id");
    container.addAll(EnumSet.allOf(MFContactType.class));

    return container;
}

}
[/code]MFContactType enum

[code]
package aaptimize.enumType;

public enum MFContactType {

/*
 * Primary
 */
PRIMARY("CN"),
/*
 * EDI
 */
EDI("EA");

protected final String value;

MFContactType(String v) {
    value = v;
}

public String getCaption() {
    return name();
}

public int getId() {
    return ordinal();
}

public String value() {
    return value;
}

public static MFContactType fromValue(String v) {
    for (MFContactType c : MFContactType.values()) {
        if (c.value().equals(v)) {
            return c;
        }
    }
    throw new IllegalArgumentException(v);
}

public static MFContactType fromName(String v) {
    for (MFContactType c : MFContactType.values()) {
        if (c.name().equals(v)) {
            return c;
        }
    }
    throw new IllegalArgumentException(v);
}

@Override
public String toString() {
    return name();
}

}
[/code]EntityAccountContact bean

package aaptimize.domain;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.eclipse.persistence.annotations.ConversionValue;
import org.eclipse.persistence.annotations.Convert;
import org.eclipse.persistence.annotations.ObjectTypeConverter;

import aaptimize.enumType.MFContactType;


/**
 * The persistent class for the entity_account_contact database table.
 * 
 */
@Entity
@Table(name="entity_account_contact")
@NamedQuery(name="EntityAccountContact.findAll", query="SELECT e FROM EntityAccountContact e")
public class EntityAccountContact implements Serializable {
    private static final long serialVersionUID = 1L;
        
    @ObjectTypeConverter(name = "contactTypeConv", objectType = MFContactType.class, dataType = String.class, conversionValues = {
        @ConversionValue(objectValue = "PRIMARY", dataValue = "CN"),
        @ConversionValue(objectValue = "EDI", dataValue = "EA") })
    
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private long id;

    @Column(name="contact_name")
    private String contactName = "";
    
    @Enumerated(EnumType.STRING)
    @Basic
    @Convert("contactTypeConv")
    @Column(name="contact_type")
    private MFContactType contactType;

    @Column(name="created_by")
    private String createdBy;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name="created_time")
    private Date createdTime;

    @Column(name="email_address")
    private String emailAddress = "";

    @Column(name="fax_number")
    private String faxNumber = "";

    @Column(name="phone_extension")
    private String phoneExtension = "";

    @Column(name="telephone_number")
    private String telephoneNumber = "";

    @Column(name="updated_by")
    private String updatedBy;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "updated_time", nullable = false, updatable = false, insertable = false, columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP")
    private Date updatedTime;

    //bi-directional many-to-one association to EntityAccount
    @ManyToOne
    @JoinColumn(name="entity_account_id")
    private EntityAccount entityAccount;

    public EntityAccountContact() {
    }

    public long getId() {
        return this.id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getContactName() {
        return this.contactName;
    }

    public void setContactName(String contactName) {
        this.contactName = contactName;
    }

    public MFContactType getContactType() {
        return this.contactType;
    }

    public void setContactType(MFContactType contactType) {
        this.contactType = contactType;
    }

    public String getCreatedBy() {
        return this.createdBy;
    }

    public void setCreatedBy(String createdBy) {
        this.createdBy = createdBy;
    }

    public Date getCreatedTime() {
        return this.createdTime;
    }

    public void setCreatedTime(Date createdTime) {
        this.createdTime = createdTime;
    }

    public String getEmailAddress() {
        return this.emailAddress;
    }

    public void setEmailAddress(String emailAddress) {
        this.emailAddress = emailAddress;
    }

    public String getFaxNumber() {
        return this.faxNumber;
    }

    public void setFaxNumber(String faxNumber) {
        this.faxNumber = faxNumber;
    }

    public String getPhoneExtension() {
        return this.phoneExtension;
    }

    public void setPhoneExtension(String phoneExtension) {
        this.phoneExtension = phoneExtension;
    }

    public String getTelephoneNumber() {
        return this.telephoneNumber;
    }

    public void setTelephoneNumber(String telephoneNumber) {
        this.telephoneNumber = telephoneNumber;
    }

    public String getUpdatedBy() {
        return this.updatedBy;
    }

    public void setUpdatedBy(String updatedBy) {
        this.updatedBy = updatedBy;
    }

    public Date getUpdatedTime() {
        return this.updatedTime;
    }

    public void setUpdatedTime(Date updatedTime) {
        this.updatedTime = updatedTime;
    }

    public EntityAccount getEntityAccount() {
        return this.entityAccount;
    }

    public void setEntityAccount(EntityAccount entityAccount) {
        this.entityAccount = entityAccount;
    }

}

You are using a StringToIntegerConverter there, which has String as representation type and Integer as model type. But your model type is a MFContactType, not an integer. So, you’d need to implement a Converter<String, MFContactType>.

Alternatively, you could maybe just use MFContactType for the item IDs of the ComboBox items, in which case you wouldn’t even need a converter.

Thanks Marko for help. I am intrigue by the alternative, however, I am at lost how to implement it. Can you provide an exmaple?

Thanks.

Also, do you have any advice how to display the object value using combobox setvalue() during initialization?