I have some simple code which unlinks a TreeItem from a TreeTableView (from its parent TreeItem's children list), then links it back in as a new child of the prior sibling. For example, if the structure is:
A
B
C
D
E
then after the "demotion" of D, you should see:
A
B
C
D
E
After the operation, a recursive print routine shows that the children's lists have been built correctly. However, the display doesn't get updated, at least not completely. I still see node D as a sibling of B and E, not a sibling of C. Collapsing and re-expanding B corrects the display. The code to do this looks like the following (where "item" is the TreeItem being demoted, "D" above):
TreeItem<FldViewNode> parent = item.getParent ();
List<TreeItem<FldViewNode>> siblings = parent.getChildren (); // TreeItems at same level as ones being deleted
// New parent will be previous sibling of us.
int itemIx = siblings.indexOf (item); // This is where we are in parent's children
if (itemIx <= 0)
{
System.out.printf("Cannot demote first child");
return;
}
siblings.remove (itemIx); // Remove this item from parent's children's list
// Insert back as new last children of previous sibling of item
TreeItem<FldViewNode> newParent = siblings.get (itemIx - 1); // Sibling just above item to demote
List<TreeItem<FldViewNode>> newSiblings = newParent.getChildren (); // TreeItems at same level as ones being demoted
newSiblings.add (item); // Insert just after new parent's existing children as a new child
Examining newSiblings
in debugger (Eclipse) or via recursive print method shows that it is correct, and obviously it is, since collapsing/expanding shows the correct value. Some other changes, such as simply removing the node (i.e., commenting out the newSiblings.add above) do show up correctly - the removed node is shown as gone immediately without need for artificial expand/collapse to force refresh. Isn't the whole idea of ObservableList
that the TreeTableView
sees changes to the tree structure? What am I missing? :)
Any ideas? Thanks!