• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sort JTree

 
Manishkumar Modi
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have JTree which is generated dynamically, I want to sort it and display it.
 
Maneesh Godbole
Saloon Keeper
Posts: 11096
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What have you tried so far, and where are you stuck?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
after searching google, which code did you have problems with.
 
Manishkumar Modi
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11096
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the code you have posted, your tree has only one element, viz. root. You want to sort 1 element???
 
Manishkumar Modi
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i haven't complete my whole code here....
but its 2 level Tree
 
Maneesh Godbole
Saloon Keeper
Posts: 11096
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Data is coming from String which contains XML data

whole code is
 
Maneesh Godbole
Saloon Keeper
Posts: 11096
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
level wise
 
Maneesh Godbole
Saloon Keeper
Posts: 11096
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In pseudo code
Access the tree model.
Retrieve all nodes for the specified level.
Use something like Collections#sort()
 
Manishkumar Modi
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank u. i will use collection
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic