Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to get selected tree nodes not in selected order?

 
Renee Zhang
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Renee Zhang
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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>
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic