Form adding a select backed by a object


I just spent a few hours today banging my head against the wall trying to understand VAADIN selects.

I can get them to behave when they contain a list of enums but when the contain a list of objects I’m having trouble.

  1. If I just pass a list to the select,
    a) everything works fine
    b) but the display is just the classname which is not very user friendly.
    b) Then I try to set a itemCaptionPropertyId but that doesn’t doesn’t work because the default container doesn’t store properties
  2. Next I try to wrap the list with a bean container before I sent it to the select
    a) I get nice labels now
    b) but setValue blows up because it is expecting the Object but sees a BeanId.

What am I missing?


I can resolve number #1

While adding items to your select, you can set the item caption.
Your code may look like this :

for(Object o : objects) 
 select.setItemCaption(o, "The caption the user see");

Hope it’s work.


That makes sense. I guess I was looking for a less brute force method and looked right over the obvious.



Another choice for the first question could be override toString() method in the bean and return the attribute you want to see in the Select.

For the second question. I use a BeanItemContainer and I do the selection of items in selects in this way.

  1. Create a Container that extends BeanItemContainer
  2. Create the select adding the container created as Container Data Source
  3. Use setValue with the bean you want

The code looks like:

The Container

package org.example.containers;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


import org.example.domain.PersonBean;

public class PersonContainer extends BeanItemContainer<PersonBean> implements Serializable {
    private static final long serialVersionUID = 1L;

    private static final int PERSON_QUANTITY = 10;
    public PersonContainer() {
        super( PersonBean.class );

    public PersonContainer getAllPersons() {
        PersonContainer container = new PersonContainer();
        List<PersonBean> personList = DummyPersonGenerator.createPersonList( PERSON_QUANTITY );
        // Adding items as beans (PersonBean) to container
        for ( PersonBean person : personList ) {
            container.addItem( person );
        // return the container
        return container;


The Bean:

package org.example.domain;

public class PersonBean {

    String name;
    String surname;
    int age;
    public PersonBean(String name, String surname, int age) { = name;
        this.surname = surname;
        this.age = age;

    // Here the getter and setter methods
    public String toString() {
        return String.format("%s - %s", name, surname);


And finally the Select creation:

Select personSelect = new Select("Person:");
personSelect.setImmediate( true );
personSelect.setContainerDataSource( new PersonContainer().getAllPersons() );

To select an item using the bean you only should do:

PersonBean personToSelect = // the item you want
personSelect.setValue( personToSelect )

Hope it helps.