Jtree sorting nodes causes collapse of paths

2005-11-02 02:12:13 PM
I've got a tree, many nodes, many depths. I'd like to sort it. My
present algorithm is to traverse the tree and sort the list of children
of each node. Once I sort the children, obviously the indexes have
changed, so you must correct them.
I've tried:
remove all children from the parent
add the children in sorted order
If I do that using something like parent.remove and parent.insert,
thats fine as long as no children UNDER those children have been
expanded. If they have, then I'm not accounting for them and thus the
tree continues to show those children under the wrong nodes once the
sorted nodes are added.
If I do that using something like model.removeNodeFromParent and
model.insertNodeInto, then the entire tree will collapse up to the node
sorted on.
Either way sucks!
I've also tried passing Arrays.sort my own comparator that, depending
on the compare outcome, will do the removeFromParent and insertNodeInto
on the fly. This results in NOT collapsing the entire tree (probably
because all nodes are never removed) however it seems like on a
descending sort the nodes get messed up. I assumed this was because I
was reordering them on the fly in the tree...but now that I'm thinking
about it, maybe I was doing something else wrong.
In any case, is there a standard proceedure for doing this? Why does it
seem so difficult? I've spent a good two days on this. Crazy!