The parameter to AbstractSelect.addItem(Object) is the item ID and the return value is the created Item. Note that this differs from AbstractSelect.addItem(), which generates an ID and returns it.
The parameters to setParent(…) must be item IDs, not Items. An item ID can have any type, though, so the formal parameters are of type Object, which is why the compiler does not complain about your second version.
In theory, item IDs for one container could even be Items of the same or another container or Items not bound to a container, but I cannot think of any use case right now where you would want to do so.
Thanx for your answer, but i still dont know how to do this.
Why i want to do:
I Have a Tree Like this:
Tree
-TopLevel
--Secondlevel
---Thirdlevel
and i just want to add an element on runtime on one of these level, thats why i wanted to save the nodes.
I runtime i just could do thrirdLevel.AddItem… or something like this…
You need to save the IDs that you use, whatever they are, and not the Items. You can always get the Items from the Tree when you have the ID.
Understanding the difference and the use of an Item and its ID is really crucial for using Vaadin data model. The ID identifies the Item in a container, but does not need to be in any way a part of the Item - the same Item could in theory be in different containers with different IDs in each, although none of the standard containers allow adding an already constructed Item to them. What you need to store in 95% of cases is the ID, not the Item.
In many cases, the Item IDs should actually be what logically identifies the items in your application, so often you don’t even need to store them separately anywhere but rather use the identifying information you already have as item IDs. They usually need to have correctly implemented equals() and hashCode() methods, though.
Note that item captions (the text shown in a UI component) are often by default the item IDs to make the simplest cases simpler, but in general the caption can be and often is something else. It can come e.g. from a property of the Items or be explicitly set in the Tree etc.
Note also that the IDs must be unique. If you can have the same text in multiple locations in the tree, you need to somehow generate unique IDs for them and then set the captions for them separately, or use a container property as captions. See the javadoc for setItemCaption(…), setItemCaptionMode(…) and setItemCaptionPropertyId(…).
You can let the Tree generate IDs for you by calling addItem() if you prefer without parameters and storing the return value - in that case, it is the generated ID and not the Item.
Thank you for your detailed explanation.
I think i understood how the data model worked.
But i still didn’t figure it out, how to slove my problem in the Code.
Could you please give me a little example, how i have to do this in my little example ?
topTreeItemId = "Toplevel";
// note that the return value of addItem(id) is NOT USED here!
tree.addItem(topTreeItemId);
secondTreeItemId = "SecondLevel"
tree.addItem(secondTreeItemId);
tree.setParent(secondTreeItemId, topTreeItemId);
...
// if the you want to use something else than the item IDs as the caption shown in the tree:
tree.setItemCaption(topTreeItemId, "The shown caption for item with ID "+topTreeItemId);
For other options for captions etc,
read the javadoc of the methods I mentioned in my previous post.