Douglas2
(Douglas Ferguson)
November 18, 2011, 12:22am
1
Hi,
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.
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
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?
Douglas
Eric13
(Eric Roberge)
November 18, 2011, 1:01am
2
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.addItem(o);
select.setItemCaption(o, "The caption the user see");
}
Hope it’s work.
Éric
Douglas2
(Douglas Ferguson)
November 18, 2011, 1:40am
3
That makes sense. I guess I was looking for a less brute force method and looked right over the obvious.
Thanks.
Javier1
(Javier Serrano)
November 18, 2011, 7:34am
4
Hi,
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.
Create a Container that extends BeanItemContainer
Create the select adding the container created as Container Data Source
Use setValue with the bean you want
The code looks like:
The Container
package org.example.containers;
import java.io.Serializable;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.vaadin.data.util.BeanItemContainer;
import org.example.domain.PersonBean;
import org.example.data.utils.DummyPersonGenerator;
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) {
this.name = name;
this.surname = surname;
this.age = age;
}
// Here the getter and setter methods
@Override
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.
Javi