Important Notice - Forums is archived
To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.
Having trouble getting combobox.select() to work
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.
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?
For a BeanItemContainer, the item is its own item ID. You are using it's name property.
Thanks for the reply. This is my very first Vaadin app so I am trying to learn the ropes.
I am a bit confused.
When I created the combobox I set
organisationId.setItemCaptionPropertyId("name");
and I then used a ValueChangeListener to record the id (rowid).
I followed your suggestion and set
organisationId.select("id");
and also tried
organisationId.select(activity.getOrganisationId());
but neither worked. What am I doing wrong?
It was only the last line in your above code sample that was the issue.
organisationId.select(mOrgID);
takes a String but needs an instance of Organization that is present in your container because the container is of type BeanItemContainer<Organisation>. The method setItemCaptionPropertyId ist just for presentation. It doesn't affect what is considered the item ID. In this case the value of each Organization's name is used to populate the combo box.
So in the absence of knowing more details the following should work
Organization org = null;
for (Organisation mOrg : orgList) {
if (mOrg.getRowid().equals(activity.getOrganisationId()))
org = mOrg;
}
organisationId.select(org);