Meaningless Drivel is fun!*
The moose likes Swing / AWT / SWT and the fly likes sort jtree Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "sort jtree" Watch "sort jtree" New topic
Author

sort jtree

mark goking
Ranch Hand

Joined: Aug 18, 2009
Posts: 155
i successfully sorted my jtree alphabetically by implementing comparable in the defaultmutabletreenode but i am having a problem with a restriction in the 1st level.

if the node is labeld "CAR" it should always stop at the top (1st node) and if the label is HOUSE it should always stay as the last node of the first level nodes. the rest of the nodes will be sorted alphabetically.

how to do this within the compare() method in a short way. thanks.

currently it works if i manually do if statements if the label is CAR or HOUSE and check what the value of the compare method is. im looking for a simplified way if there is. thanks


Website/Java Games: http://www.chitgoks.com
Tech Blog: http://tech.chitgoks.com
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18153
    
    8

mark chester goking wrote:currently it works if i manually do if statements if the label is CAR or HOUSE and check what the value of the compare method is. im looking for a simplified way if there is. thanks

But that's what you said the rule was. I don't see how the implementation can be simpler than the rule.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 9993
    
    7

I am a bit confused.
Do you mean to say first level nodes under root should not be sorted, but their children should be?



[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
mark goking
Ranch Hand

Joined: Aug 18, 2009
Posts: 155
hi maneesh.
1st level nodes should be sorted, but if the string is CAR, it should always be the first node. if it is HOUSE is should always be the last node.

e.g.

root
-CAR
-- child1
-- child2
-AIRCRAFT
-- child1
-- child2
-BUILDING
-- child1
-- child2
-HOUSE
-- child1
-- child2

aircraft and building are sorted. while car and house are not. car should always be the first and house should always be the last
mark goking
Ranch Hand

Joined: Aug 18, 2009
Posts: 155
for the moment, this is how i did it



just wondrin' if there's a simplier way to simplify it ;)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: sort jtree
 
Similar Threads
jtree problem
JTree with checkbox
JTree
JTree
Sorting JTree Nodes alphabetically