i’m having trouble with customizing the itemCaption of a combobox component, it is populated with a BeanItemContainer, and I would like to have it’s itemCaption with two properties of the container at the same time.
Someone could help me please?
After a slight diversion looking into new Vaadin 7 Converters, I think the easiest approach is simply to override the ComboBox#getItemCaption method. Example follows. If it’s something you find yourself doing often enough, I reckon it might be worth creating an interface CaptionGenerator with a method getCaption(Item) and creating reusable combobox that uses an implementation to return the caption.
HTH,
Cheers,
Charles.
/* Lets create some data */
Person[] people = new Person[]
{
new Person("Fred", "Boggins", 20),
new Person("Doris", "Day", 98),
new Person("Freddy", "Flintoff", 38),
new Person("Boris", "Clinkenthorp", 52)
};
/* For simplicity, the itemId of the bean is the bean itself */
BeanContainer<Person, Person> container = new BeanContainer<Person, Person>(Person.class);
for (Person person : people) {
container.addItem(person, person);
}
ComboBox cb = new ComboBox("Pick A Person", container) {
@Override
public String getItemCaption(Object itemId) {
Item item = getItem(itemId);
if (item == null) {
return "";
}
return String.valueOf(item.getItemProperty("familyName")) + ", " + String.valueOf(item.getItemProperty("name"));
}
};