permaculture playing cards*
The moose likes Swing / AWT / SWT and the fly likes How to get selected tree nodes not in selected order? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to get selected tree nodes not in selected order?" Watch "How to get selected tree nodes not in selected order?" New topic
Author

How to get selected tree nodes not in selected order?

Renee Zhang
Ranch Hand

Joined: Sep 10, 2001
Posts: 72
By default, JTree has a method call getSelectionPaths()which returns a treePath array of all selected nodes in selection order.
For example. I have a tree and there are 5 leaf nodes and the order is
nodeA
nodeB
nodeC
nodeD
nodeE
if I select nodeE first then select nodeA, I will get nodeE's path first, then nodeA's path if I call getSelectionPaths(). I am wondering if there is any methods that returns selected treePath in index order? I don't want to travel through the whole tree to see which node is selected because I have a huge data tree.
Thanks in advance.
Renee
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

The following line of your post doesn't make sense...

I don't want to travel through the whole tree to see which node is selected because I have a huge data tree.


You can call getLastPathComponent() on each TreePath object to get the selected element...


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Renee Zhang
Ranch Hand

Joined: Sep 10, 2001
Posts: 72
Thanks Nathan, After I call getLastPathComponent(), I will get a treeNode array which is ordered by selection (The first selected node will be the first element in the array. In the previous example, it's {[NodeE],[NodeA]} ). But I want to get a treeNode array which is ordered by index(which is the closest to the parentNode.(In the previous example, it's {[NodeA],[NodeE]}).
Thanks in advance.
Renee
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Sorry... What I really meant to ask you was why you need to know the distance from the parent component, and what requires you to "travel through the whole tree to see which node is selected"... Even though information exists in the TreeModel, the JTree, and the TreeNode itself that would help you discover this, it would require a fair amount of processing per node... it would be much simpler to base whatever you need to do to the node on the node itself that is returned to you...
Renee Zhang
Ranch Hand

Joined: Sep 10, 2001
Posts: 72
Hi Nathan,
I finally find a easy way to do it. The order is very important to me because I want to cut/copy/paste the exactly same order to another folder node. Please let me know if you have better solution and thanks again for your time.
<code>
import java.awt.*;
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.JTree.*;
import java.awt.event.*;
import javax.swing.event.*;

public class SelectableTree extends JFrame {
private JTree tree;
private JButton showSelectionButton = new JButton("Show Selection");
public static void main(String[] args) {
new SelectableTree();
}
public SelectableTree() {
super("JTree Selections");
Container content = getContentPane();
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
DefaultMutableTreeNode child;
DefaultMutableTreeNode grandChild;
for(int childIndex=1; childIndex<4; childIndex++) {
child = new DefaultMutableTreeNode("Child " + childIndex);
root.add(child);
for(int grandChildIndex=1; grandChildIndex<6; grandChildIndex++) {
grandChild =
new DefaultMutableTreeNode("Grandchild " + childIndex + "." + grandChildIndex);
child.add(grandChild);
}
}
tree = new JTree(root);
tree.getSelectionModel().setSelectionMode(javax.swing.tree.TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
content.add(new JScrollPane(tree), BorderLayout.CENTER);
showSelectionButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
TreePath[] treePaths = tree.getSelectionPaths();
int [] treeRows = tree.getSelectionRows();
for (int i = 0; i < treePaths.length; i++) {
DefaultMutableTreeNode Node
= (DefaultMutableTreeNode)treePaths[i].getLastPathComponent();
System.out.println(Node.toString());
}
System.out.println("Before sorting");
for (int i = 0; i < treeRows.length; i++) {
TreePath treePath = tree.getPathForRow(treeRows[i]);
DefaultMutableTreeNode Node
= (DefaultMutableTreeNode)treePath.getLastPathComponent();
System.out.println(Node.toString());
}
System.out.println("After sorting");
java.util.Arrays.sort(treeRows);
for (int i = 0; i < treeRows.length; i++) {
TreePath treePath = tree.getPathForRow(treeRows[i]);
DefaultMutableTreeNode Node
= (DefaultMutableTreeNode)treePath.getLastPathComponent();
System.out.println(Node.toString());
}
}
});
content.add(showSelectionButton, BorderLayout.SOUTH);
setSize(250, 275);
setVisible(true);
}
}
</code>
 
Don't get me started about those stupid light bulbs.
 
subject: How to get selected tree nodes not in selected order?
 
Similar Threads
Select a node in Jtree
JCheckBox in Jtree Problem
how to get the mouse's position
TreePath exception
Node Selection in JTree