Important Notice - Forums is archived
To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.
set parents in treetable
Hi:
I have a question regarding treetables...I'm using the following code to set the parents of the treetable items and it doesn't work...any ideas?...
public TreeTable prueba = new TreeTable();
...
prueba.addContainerProperty("Name", String.class, "");
prueba.addContainerProperty("Father", String.class, "");
for (int i = 0; i < contenedorUsers.size(); i++) {
Object id = contenedorUsers.getIdByIndex(i);
Item item = contenedorUsers.getItem(id);
prueba.addItem(new Object[] {item.getItemProperty("Name").getValue().toString(),
item.getItemProperty("Father").getValue().toString()},null);
}
After this, I get a treetable with two columns, one with the name and one for the father's name...after that, I do:
for (int i=1; i<=prueba.size();i++){
Object name = prueba.getItem(i).getItemProperty("Name");
Object father = prueba.getItem(i).getItemProperty("Father");
prueba.setParent(name, father);
prueba.setCollapsed(name, false);
}
After this, nothing happens, and I only have the table with two columns but with no hiearchy on it....if I do this item by item (they are not too many yet)...I get a perfectly goo treetable....
Any idea on why is this failing???...thanks!!!!
setParent(child, parent) works with item id's. You seem to be using integers as item id's. This is because your line:
prueba.addItem(new Object[] {item.getItemProperty("Name").getValue().toString(),
14 item.getItemProperty("Father").getValue().toString()},null);
The null in the end does that. From the javadocs for Table.addItem(Object[] cells, Object itemId):
itemId - the Id the new row. If null, a new id is automatically assigned. If given, the table cant already have a item with given id.
so the table creates an id that is int.
You code would work if you'd call something like prueba.setParent(4,2);
What you probably would like to do is use manual item id's. Something like
Item pruebaItem = prueba.addItem(item.getItemProperty("Name").getValue());
pruebaItem.getItemProperty("Name").setValue(item.getItemProperty("Name").getValue());
pruebaItem.getItemProperty("Father").setValue(item.getItemProperty("Father").getValue());
Then you can use something like
prueba.setParent("Steve", "Bob");
or just use the item id's that you get, like prueba.getItem(i);.
Your copying of items from one table to another looks like a weird thing to do. A little hard to say as I just see a fragment of the code. Also, it is a good practice to use only English in code (referring to prueba). It would be easier to help that way.
Jens Jansson:
Your copying of items from one table to another looks like a weird thing to do. A little hard to say as I just see a fragment of the code. Also, it is a good practice to use only English in code (referring to prueba). It would be easier to help that way.
Making things more complicated than they should be is like a gift I have....
thank you very much for your help, it worked perfect!!!
regards,
Hugo