Sort JTree

Hello,

I was wondering if there is a way to sort a JTree after nodes have been added. Any help is much appreciated. Thanks.

vyang

[148 byte] By [vyanga] at [2007-11-27 10:42:58]
# 1

It all depends on how you are storing information in JTree.

Do you use model? If you do, how are you implementing it?

Do you use any collection to store nodes? how?

All you need to do, is to write sorting mechanism and then when you insert new node, just fire proper event like treeStructureChanged(...) or nodeChanged(...).

Let me know what you think about it.

Regards,

D

dhaval04a at 2007-7-28 19:21:42 > top of Java-index,Desktop,Core GUI APIs...
# 2

Hello,

I'm using a DefaultTreeModel to set my JTree. Since we are talking about JTrees, my JTree has some default value preloaded. I'm using Netbeans GUI builder. Is there a way to remove these values?

vyanga at 2007-7-28 19:21:42 > top of Java-index,Desktop,Core GUI APIs...
# 3

This is a simple function that can sort the child nodes of a specifed parent node defined by the TreePath argument.

Copy and modify this code to suite your app. This only sorts on node but it can easily be modified to sort the entire table using recursion.

public void sortTreeNode(TreePath editPath) {

DefaultMutableTreeNode node = (DefaultMutableTreeNode)editPath.getParentPath().getLastPathComponent();

Enumeration<DefaultMutableTreeNode> children = (Enumeration<DefaultMutableTreeNode>)node.children();

Vector<DefaultMutableTreeNode> childNodes = new Vector<DefaultMutableTreeNode>();

Vector<String> nodeNames = new Vector<String>();

while( children.hasMoreElements() ) {

DefaultMutableTreeNodechild = children.nextElement() ;

nodeNames.addElement( child.getUserObject().toString() );

childNodes.addElement( child );

}

Collections.sort(nodeNames);

Vector<DefaultMutableTreeNode> sortedChildNodes = new Vector<DefaultMutableTreeNode>();

for(String nodeName: nodeNames ) {

for(DefaultMutableTreeNode child: childNodes) {

if(child.getUserObject().equals(nodeName)) {

sortedChildNodes.addElement( child );

}

}

}

node.removeAllChildren();

for(DefaultMutableTreeNode child: sortedChildNodes) {

node.add( child );

}

((DefaultTreeModel)libTree.getModel()).nodeStructureChanged( node );

childNodes.removeAllElements();

childNodes = null;

}

For your second problem, you need to consider these methods: the remove( . . ) methods under DefaultMutableTreeNode and the nodeStructureChanged(..) method in DefaultTreeModel. It is very simple to tweak and use those methods to get the results you require.

ICE

icewalker2ga at 2007-7-28 19:21:42 > top of Java-index,Desktop,Core GUI APIs...
# 4

This approach is quick and easy way to achieve goal. But it is not the best solution. If you have many children under one node, it may cause a little performance issue.

When I use JTree, I made my own model and my own custom object representing node. Technically JTree is just a view of your tree like data structure.

I don't have much idea about your design so I won't able to comment much. But if you have very short tree and if you do not care about model or possibilities of expanding your code to some serious design, icewalker2g has perfect solution for you.

If you are thinking about good design, use event firing and update model to reflect view strategy. I mean only repaint tree to reflect proper content.

Regards,

D

dhaval04a at 2007-7-28 19:21:42 > top of Java-index,Desktop,Core GUI APIs...
# 5

I totally agree with D, that this is not the very best solution for large tree sorting. I used this approach cause in my app, the sorting is done for only the Children of one TreeNode at a time.

This may be over your head in terms of code, but you can look at the source code provided for the JTreeTable examples on the Swing tutorials site, you'll find a good example of what D is talking about, a TreeModel with custom TreeNodes that enable sorting at the model level.

Check out: http://java.sun.com/products/jfc/tsc/articles/treetable1/index.html

http://java.sun.com/products/jfc/tsc/articles/treetable2/index.html

ICE

icewalker2ga at 2007-7-28 19:21:42 > top of Java-index,Desktop,Core GUI APIs...
# 6

Hello,

Thanks guys, for now its going to be something simple, but it will grow to something a little more in depth so I guess I have to do some custom coding.

vyang

vyanga at 2007-7-28 19:21:42 > top of Java-index,Desktop,Core GUI APIs...
# 7

Well vyang,

You have very good idea about how to do sorting via ICE's reply. If you need example of what I did it, let me know at dhaval04@yahoo.com. I will be happy to give you code.

I also thanks ICE for his help and glad that we both can able to help you.

Regards,

D

dhaval04a at 2007-7-28 19:21:42 > top of Java-index,Desktop,Core GUI APIs...