Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Swing / AWT / SWT and the fly likes sorting nodes in JTree Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "sorting nodes in JTree" Watch "sorting nodes in JTree" New topic
Author

sorting nodes in JTree

pavan in
Ranch Hand

Joined: Oct 22, 2002
Posts: 64
I have a JTree. Is it possible to re-arrange (or sort) the chldren below a parent when the node's display id is changed?
Say,
A1
A11
A22
When, A22 id is changed to A10, it should appear above A11 and not below it. If A22 had children before, they should also be moved along with A22.
Can I rearrange the nodes as per the node id?
Thanks.
Bill Liteplo
Ranch Hand

Joined: Oct 16, 2002
Posts: 88
Sure. Your tree model should be based on the IDs. If your objects are keeping track of the parenting, then they will have to manage all of that. It depends how you set it up. Just implement the right structure, make some listeners for when the ID is changed, and tell the tree to update itself when you are done.
Simple as rocket science.
Bill
pavan in
Ranch Hand

Joined: Oct 22, 2002
Posts: 64
Originally posted by Bill Liteplo:
Sure. Your tree model should be based on the IDs. If your objects are keeping track of the parenting, then they will have to manage all of that. It depends how you set it up. Just implement the right structure, make some listeners for when the ID is changed, and tell the tree to update itself when you are done.
Simple as rocket science.
Bill

Thanks Bill.
I am using DefaultTreeModel.If I create my own defaulttreemodel, which methods should I override?
Martin Schneider
Ranch Hand

Joined: May 16, 2002
Posts: 42
Instead of DefaultMutableTreeNode you can make SortableTreeNode. Those object you are putting in tree must implement Comparable.

Basically you just call root.sort(); and that's all. Hope this works
pavan in
Ranch Hand

Joined: Oct 22, 2002
Posts: 64
Thanks Martin.
Will the program you sent me work on update too. Say, my node userobject has sequence number in addition to node id. if the user modifies a nodes sequence number,then it's siblings sequence number are changed too (programmatically, model code of MVC). When the sequence numbers are changed, the tree has to restructured. will calling sort method do that?
What happens if the node already has children?Will they be moved along with their parent?
Thanks.
Martin Schneider
Ranch Hand

Joined: May 16, 2002
Posts: 42
I think you have to call method sort recursively because it sorts only child nodes of current parent.
You have a sequense number and id. That sort works, I guess, only for the names nodes display so you combine those two number in your node-object.
pavan in
Ranch Hand

Joined: Oct 22, 2002
Posts: 64
Martin,
In sort() you remove the node and add it again.But, once I delete the node from the tree, the children are also removed. then, when I add the node at the sepcific index, it'll not have any children and I'll have to add them again.right? I dont want to delete the node but still move it to a specific index once the user has updated node info.
Thanks.
ouldache oussama
Greenhorn

Joined: Dec 03, 2012
Posts: 2
It is verry simple :
Put all Leafs of a folder in to an array (named hear o_people)


Simple !!! isn't it ?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: sorting nodes in JTree
 
Similar Threads
Updating a JTree
JTree Scalablity ???
Dynamic JTree
JTree in Browser
sorting jtree