Select with BeanItemContainer

I have a Select that uses a BeanItemContainer that is used as the dataSource of my select.

For this example, lets say that my bean is composed of id, name and email. I was able to set the caption of my itens using


it works perfectly.

My trouble is that when I try to select one item, I need to make a “for” on all the itens to see wich item matches the id I have to select the item from the for.
I tried using hashcode and equals but it did not work. Is there an easier way to select the id I have between all the beans I have in my select?

tkz in advance

Andre Jarussi

getValue should in my opinion always return the item id, so in a BeanItemContainer that is the Java Bean itself. Using setItemCaptionPropertyId shouldn’t change that. Are you getting that, or are you getting the description instead? What is select.sltLinhaOrigin?

To clarify this, tell me exactly what you get and what you would like to get.

Tkz for the reply Jens…

I’ll try to make myself more clear …

Let’s say I have a list of States in a database and I use it to populate the BeanItemContainer and use the BeanItemContainer to populate my select.

Now I have an user that has a selected state and I want to take that information from the database and select the user state in my ComboBox.
I’ll try to shorten my code:

 BeanItemContainer<StateBean> containerState = new BeanItemContainer<StateBean>(StateBean.class);
sltState = new Select("State:", containerState);
public class StateBean {
	private String id;
	private String abbreviation;
	private String fullName;
... getters anda setters ...

so now I have a Select filled with beans of the type State. I have a user that has on his info recorded in the database that he is from the state with the id 10 and I would like to make sure I’m able to select that state in the selectBox
something like"my state that has the id 10)

the only way that I was able to get the item with taht id is doing a for in my select searching for the item that has that id. I tried using hashcode and equals, creating a new item with the same values and all that…

sorry for the long text and tkz again!

Allright, so you have the string identifier of a bean and you want to find the bean that it belongs to. I guess that should be a fairly standard Java (also non-Vaadin) problem. If I recall correctly the containers should use equals to evaluate if the beans are the same, so implementing equals and hashcode is the way to go. I would suggest you try to do that again, and see that your hand made State object .equals() the container object (that it returns boolean true).

The BeanItemContainer does not support that one, but here is a solution using the

// Some sample beans
ArrayList<StateBean> beans = new ArrayList<StateBean>();
beans.add(new StateBean("id1", "1", "First")); // id, abbreviation, stateFullName
beans.add(new StateBean("id2", "2", "Second"));
beans.add(new StateBean("id3", "3", "Third"));

// Create a Collection container using id property as the key
CollectionContainer containerState = CollectionContainer.fromBeans(beans, "id");		

// Select using the container and "stateFullName" property as caption
Select sltState = new Select("State:", containerState);

// Select the first bean using its id

Tkz a lot for all the replies!!

The CollectionContainer did the trick … it was exactly what I was looking for !! It works just like a hashCode and equals inside a list!

5 star rated for the addon!

tkz for all the replies!

With BeanItemContainer it will works too.

If you Bind your container to some property on an Item, when you do a setValue on the Select, it will select the correct bean.

But, you will need to implement hashcode and equals correctly.

Caution with JPA beans. In general, it can be some kind of proxy, and compare classes will result on false results of equals, for example.