This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
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 The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Sort JTree" Watch "Sort JTree" New topic
Author

Sort JTree

Manishkumar Modi
Greenhorn

Joined: Sep 23, 2011
Posts: 14
I have JTree which is generated dynamically, I want to sort it and display it.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10268
    
    8

What have you tried so far, and where are you stuck?

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

Joined: Jun 09, 2003
Posts: 4632
after searching google, which code did you have problems with.
Manishkumar Modi
Greenhorn

Joined: Sep 23, 2011
Posts: 14
I used following code
protected DefaultTreeModel m_model;
DefaultMutableTreeNode rootTree = new DefaultMutableTreeNode("My Root", true);
m_model = new DefaultTreeModel(rootTree);
tree = new JTree(m_model);

so what code should i write to sort my tree
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10268
    
    8

From the code you have posted, your tree has only one element, viz. root. You want to sort 1 element???
Manishkumar Modi
Greenhorn

Joined: Sep 23, 2011
Posts: 14
i haven't complete my whole code here....
but its 2 level Tree
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10268
    
    8

So how do you want the sorting to be? Level wise? All data?
You mentioned earlier,
I have JTree which is generated dynamically
Where is the data coming from? DB? If yes, have you considered ordering the data when you retrieve it through a query?


Manishkumar Modi
Greenhorn

Joined: Sep 23, 2011
Posts: 14
Data is coming from String which contains XML data

whole code is
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10268
    
    8

In future, while posting code, please UseCodeTags. I have added them this time. As you can see it makes the code much more easier to read and understand.
Maneesh Godbole wrote:So how do you want the sorting to be? Level wise? All data?

You want to tell us about this?

Manishkumar Modi
Greenhorn

Joined: Sep 23, 2011
Posts: 14
level wise
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10268
    
    8

In pseudo code
Access the tree model.
Retrieve all nodes for the specified level.
Use something like Collections#sort()
Manishkumar Modi
Greenhorn

Joined: Sep 23, 2011
Posts: 14
Thank u. i will use collection
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Sort JTree