Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

How to get selected tree nodes not in selected order?

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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>
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic