aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Sorting JTree Nodes alphabetically Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Sorting JTree Nodes alphabetically" Watch "Sorting JTree Nodes alphabetically" New topic
Author

Sorting JTree Nodes alphabetically

Suganthi Velliah
Greenhorn

Joined: Apr 30, 2007
Posts: 17

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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Apr 30, 2007
Posts: 17
thanks Paul. I will try this
ramswaroop ram
Greenhorn

Joined: Oct 25, 2011
Posts: 8
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

Joined: Oct 25, 2011
Posts: 8
I have a problem in sorting nodes.
please post the code for sorting nodes.

Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> 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

Joined: Mar 10, 2008
Posts: 326

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

Joined: Oct 25, 2011
Posts: 8
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

Joined: Oct 25, 2011
Posts: 8
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

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 ?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sorting JTree Nodes alphabetically