How to add new item into treetable as first root node?


I’m wondering if it’s possible to add node into treetable as first root node? I have a huge tree with timestamped nodes and I would like to add the newest nodes as first in this treetable.

Similar way as method TreeTable.addItemAfter(Object previous, Object newItem) I would like to do the same trick as methods TreeTable.addItemBefore(Object nextItem, Object newItem) or TreeTable.addItemAsFirst(Object newItem) would do if they’d existed.

I guess as a workaround I could reorganize items inside the container datasource (HierarchicalContainer) and repaint the whole treetable but I would prefer some more reasonable solution for this.

When the item’s parentId==0 then it’s a root.

Would have to then go through all the children first level of that new item only and change their parentId to the new item’s id.

That should do it.


TreeTable uses HierarchicalContainer by default and it has addItemAt(). Unfortunately, it doesn’t show in the TreeTable API, as it only requires Hierarchical and Ordered, neither of which allows adding an item as first.

So, you can do:


HierarchicalContainer also has moveAfterSibling(), which you can use to move an old root item after a new one, after you have added it.

This may look odd but:

container.moveAfterSibling(beanItem, targetItemId);
container.moveAfterSibling(targetItemId, beanItem);

…adds the beanItem before the targetItemId, regardless where that item is. Well, the addItemAt() is probably easier in most cases.

Thanks for the answer!

I’ve tried both solutions but I still cannot get my tree to work. The only difference between usage of addItemAt() -solution is that I use method where I can define the item id. This doesn’t seem to work for TreeTable:

Item newItem = ((HierarchicalContainer)getContainerDataSource()).addItemAt(0, myTreeNodeObject.getId()); 
newItem.getItemProperty("treeNodeObject").setValue(new MyTreeNodeObject());

There is no error message and new item doesn’t appear into treetable.

I debugged what actually happens inside of the tree’s HierarchicalContainer and it looks like the “newItem” is added into HierarchicalContainer.allItemIds but not into HierarchicalContainer.roots which to my understanding should contain all root level item ids?

I also tried the sibling moving solution but this code:

Item newItem = ((HierarchicalContainer)getContainerDataSource()).addItemAt(0, myTreeNodeObject.getId()); 
newItem.getItemProperty("treeNodeObject").setValue(new MyTreeNodeObject());
Item firstRootLevelItem = getItem(((HierarchicalContainer)getContainerDataSource()).getIdByIndex(0));
((HierarchicalContainer)getContainerDataSource()).moveAfterSibling(newItem, firstRootLevelItem);
((HierarchicalContainer)getContainerDataSource()).moveAfterSibling(firstRootLevelItem, newItem);

But this solution throws IllegalArgumentException “Given identifiers no not have the same parent.”. This might work on branch level but on root level there seems to be a problem.

I have to note that I’m running Vaadin version 6.6.8 and I’m not sure if there has been fixes to this issue lately.

Sorry, I was careless. I gave tree items instead of tree item IDs to moveAfterSibling()-method, which works exactly the way it supposed to work so THANKS FOR THE SOLUTION!

Still I didn’t have luck with the addItemAt(0, itemId)-method, for some reason it’s just not working for my tree table.

it seems after four years, that the addItemAt is not working for the treeTable i think this is a unreported bug.

The bug is reported after all: