Extremely basic Tree question

I feel really stupid but I can’t figure out how to create a tree menu from HierarchicalContainer of pojo’s, where caption of the node is the name property of the pojo :*) I am missing something really really basics of Vaadin containers…


public class Pojo {
                     private String name = "New Pojo";
	
	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}
}

Basically , you do

		 
final String CAPTION = "Caption"; //can be any string
HierarchicalContainer hc = new HierarchicalContainer();
hc.addContainerProperty(CAPTION, String.class, null);
tree.setContainerDataSource(hc);
tree.setItemCaptionPropertyId(CAPTION);

and then you add item this way


hc.addItem(ITEMID).getItemProperty(CAPTION).setValue("leaf 1"); 

Thanks, I managed to do that much, but it leaves my Pojos out of the tree. I want to have the HierarchicalContainer filled with Pojos. And then have the name of the Pojo as caption. Or is that even possible? Should I just fill it with id’s and then based on mouse clicks fetch the correct Pojo from some repository?

Perhaps you need a BeanItemContainer that is heirarchical ? See ContainerHierarchicalWrapper

You could do easily do


hc.addItem(pojoInstance).getItemProperty(CAPTION).setValue(pojoInstance.getName()); 

Here the pojoInstance is the id.

Hope it helps.

That’s exactly what I was looking for! Thanks :slight_smile: