Mimzo
Mimzo

Reputation: 69

Vaadin combobox.select() does not work no matter what I do

I am writing a basic CRUD app using Hibernate MySQL. Adding new records is fine but I'm having some trouble getting an existing record's value to appear as the selected item by default when editing an existing record. Here is the relevant code:

Criteria criteriaz = session.createCriteria(Organisation.class);
final List<Organisation> orgList = criteriaz.list();
BeanItemContainer<Organisation> srcOrgs = new BeanItemContainer<Organisation>(Organisation.class);
srcOrgs.addAll(orgList);

organisationId.setInvalidAllowed(false);
organisationId.setNullSelectionAllowed(false);
organisationId.setContainerDataSource(srcOrgs);
organisationId.setItemCaptionMode(ItemCaptionMode.PROPERTY);
organisationId.setItemCaptionPropertyId("name");

for (Organisation mOrg : orgList) {
                    if (mOrg.getRowid().equals(activity.getOrganisationId())) 
                        mOrgID = mOrg.getName();  
                }

organisationId.select(mOrgID);

What am I doing wrong here?

Upvotes: 0

Views: 120

Answers (2)

Murat Karag&#246;z
Murat Karag&#246;z

Reputation: 37604

Although you set the caption property id to name it does not change the identifier ID for the items. So you can use the following on conjunction with BeanItemContainer:

organisationId.select(orgList.get(mOrgID));

and change this

mOrgID = mOrg.getName(); 
to
mOrgID = mOrg; 

To make @Morfic happy:

organisationId.select(orgList.get(orgList.indexOf(mOrgID)));

Upvotes: 1

agad
agad

Reputation: 2189

Use just:

for (Organisation mOrg : orgList) {
    if (mOrg.getRowid().equals(activity.getOrganisationId())) 
        organisationId.select(mOrg);
        break;
    }

BeanItemContainer contains beans as IDs, so you always work with beans only.

Upvotes: 1

Related Questions