Currently (AFAIK), the toString() method is used to display items in a component (e.g. Tree). This can be overridden by using setItemCaptionProperty().
But assume the following:
You want to display the first- and lastname of a person while still having a firstName and lastName field in your class. setItemCaptionProperty() doesn’t help here.
I did a workaround like this:
for ( Object itemId : container.getItemIds() ) {
BeanItem beanItem = ( BeanItem ) container.getItem( itemId );
Person person = ( Person ) beanItem.getBean();
component.setItemCaption( itemId, person.getFirstname() + " " + employee.getLastname() );
}
But this still isn’t a good solution: When somewhere an item is added to the container, the toString() is used again.
Now you may say, I could simply override the toString(), but this isn’t an option as the Person class is generated by JAXB.
The solution would be that the components have a method that can be overridden, like this:
public String captionFor( itemId )
Another suggestion of Wolfie in IRC was giving something like an ItemCaptioner to the component:
ItemCaptioner captioner = new ItemCaptioner(){
public String captionFor(Item item) {
return "foo";
}
}
component.setCaptioner( captioner )
I’d also prefer Wolfies solution, because this means you don’t have to extend such a component.
I created a ticket:
http://dev.vaadin.com/ticket/3238