Trees and Containers

I’m missing a key concept related to trees and data sources.

I need a tree that displays complex objects rather than just strings.

As an example let’s say I have a class Person:

public class Person
    private String name;
    private String id;
    // getters and setters

I’ve extended HierarchicalContainer and added a method to create people and add them. So far I think I have things correct.

However, adding people to the tree results in the tree displaying that object id. What I need is to be able to display the name from that object’s person field. When someone clicks on an element in the tree I’d then like to be provided with the Person object mapping to that element they clicked on, or the id that I can then look up myself in a hash.

I guess I should have tried this from the beginning… a toString on the data object works, dunno if that’s the right way and I’m not positive where captions fit in but it works.
Is there a way to implement my tree and container pair such that I can tell the tree “use this field from each item for display”?

I can see it has something to do with setting the caption, but it’s not clear where I do this. Appropriate calls appear on the Tree itself, but that doesn’t seem to line up with using a data source.

Something in the lines of this

HierarchicalContainer container = new HierarchicalContainer();

// Make sure you have properties defined in your container
container.addContainerProperty("name", String.class, null);

addPersonToContainer(container, new Person("Jeremy German"));


// Define which property is going to be used as the item's caption in the tree


public static void addPersonToContainer(Container container, Person person) {
   Item item = container.addItem(person);
   // Item is null if the person is already in the container
   if(item != null) {

When you select an item in the tree, an event is dispatched. The event will tell you the item id of the item which was clicked. In this case, the item id is the person object. You’ll need to case the itemId to a Person, Person person = (Person) event.getItemId();