Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sorting JTree Nodes alphabetically

 
Suganthi Velliah
Greenhorn
Posts: 17
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi All,

i want to sort the nodes of Jtree alphabetically. I have tried but it is not working.

My tree structure is as follows:

Root Node
|
Parent Node 1
|
Child 1
Child 2
.....
Child n
Parent Node 2
|
Child 1
Child 2
.....
Child n

.....
Parent Node n

The tree is featured with a popup which appears while right clicking on the node which gives options for adding a node, removing a node and renaming a node.

So whenever a node is added or removed or renamed I want to display the nodes in a sorted order. I want to sort the nodes alphabetically at branch level.

ie. Parent nodes level and child nodes level.

It would be very helpful if you provide me with some good way to do this.
Thanks in advance
 
Paul Clapham
Sheriff
Pie
Posts: 20945
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I did when I had that problem was to just keep the nodes in alphabetic order from the beginning. When I added a node, I just went down the list until I found something later in the alphabet than the new node, and added the new node before it. Or added the new node at the end, if I didn't find something. And for changed nodes, you simply remove the node from the list and add it back, using the same procedure of course.
 
Suganthi Velliah
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Paul. I will try this
 
ramswaroop ram
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Suganthi,

I am trying to add and delete and sort nodes on rightclick.(jpopup) but facing some problem.
Can you please send me your code.

Regards
swaroop
 
ramswaroop ram
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a problem in sorting nodes.
please post the code for sorting nodes.

 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> please post the code for sorting nodes

the OP hasn't posted in 2+ years, so I'd say your chances of
getting his/her code are slim.

If you just want code, try rent-a-coder, or if your own code
is not working, post the code here and we'll try to point you
in the right direction.
 
Ove Lindström
Ranch Hand
Posts: 326
Android Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ramswaroop ram wrote:I have a problem in sorting nodes.
please post the code for sorting nodes.



What you need is to implement your own TreeModel.

There is a nice example of an OrderedTreeModel at http://seminars.jguru.com/forums/view.jsp?EID=1184178
 
ramswaroop ram
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Ove Lindström for the link.
do we need to import some library to implement OrderedTreeModel
or do we need to create our own.


 
ramswaroop ram
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it has to be created then please post getComparator() meathod.

Here is my code:
if(actionCommand.equals("Sort")){
DefaultMutableTreeNode lastDt=(DefaultMutableTreeNode)selPath.getLastPathComponent(); //I am using Jpopup and this selects the path of the node selected
DefaultMutableTreeNode parent = (DefaultMutableTreeNode)lastDt.getParent(); //this gives last selected path node's parent
if (parent == null) return; //from here I am using code from Ove Lindström's link
if (parent instanceof DefaultMutableTreeNode) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) parent;
ArrayList children = Collections.list(node.children());
Collections.sort(children, this.getComparator()); // --------------------------------- as OrderedTreeModel is not yet implemented i am getting error here----------------------------------
node.removeAllChildren();
Iterator childrenIterator = children.iterator();
while (childrenIterator.hasNext()) {
node.add((DefaultMutableTreeNode) childrenIterator.next());
}
}

}
 
ouldache oussama
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is verry simple :
Put all Leafs of a folder in to an array (named hear o_people)


Simple !!! isn't it ?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic