Tree doesn't allow custom children

I instantiated a Tree and I add items to it using addItem(Object). All of the nodes, even the child, are rendered at the root level.

    protected void init(VaadinRequest request) {        
        final VerticalLayout layout = new VerticalLayout();

        Tree tree = new Tree();
        Node node1 = new Node("Parent1");
        Item item1 = tree.addItem(node1);

        Node node2 = new Node("Child of P1");
        Item item2 = tree.addItem(node2);
        tree.setParent(item2, item1);
        tree.setChildrenAllowed(item2, false);

        Node node3 = new Node("Parent2");
        Object item3 = tree.addItem();
        tree.setItemCaption(item3, "Parent2");
// ....
    public class Node {
        private String name;
        public Node(String name) {
   = name;
        public String getName() {
            return name;
        public void setName(String name) {
   = name;
        public String toString() {
            return name;

If I try the same thing as above using addItem() instead of addItem(Object), then it works. But of course the tree doesn’t have my custom Node objects.

What’s wrong and how do I fix it?

It looks suspicious that you use an object like that as the item ID for the tree items. You can do that, as Tree by default uses the toString() on the item ID object for the node caption. But I hope you understand that the parameter for addItem(Object) is an
item identifier
, not the item object itself. The addItem() is a factory method that creates the item - you can give it the ID as parameter or otherwise it will use an automatically generated ID.

In your code, you are using the Item object where you should be using the item ID (your Node objects).

For example, you should use:

        tree.setParent(node2, node1);
        tree.setChildrenAllowed(node2, false);
        tree.setItemCaption(node3, "Parent2");