Win a copy of Java Persistence with Spring Data and Hibernate this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Jeanne Boyarsky
Saloon Keepers:
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Piet Souris
Bartenders:

Sorting JTree Nodes alphabetically

 
Greenhorn
Posts: 17
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 27582
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Paul. I will try this
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a problem in sorting nodes.
please post the code for sorting nodes.

 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 326
Android Mac OS X Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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());
}
}

}
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Well don't expect me to do the dishes! This ad has been cleaned for your convenience:
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
reply
    Bookmark Topic Watch Topic
  • New Topic