Returns a TreePath containing the specified node. : JTree Selection « Swing « Java Tutorial






import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;

public class Main {
 
  public TreePath getPath(TreeNode node) {
    List<TreeNode> list = new ArrayList<TreeNode>();

    while (node != null) {
      list.add(node);
      node = node.getParent();
    }
    Collections.reverse(list);

    return new TreePath(list.toArray());
  }
}








14.71.JTree Selection
14.71.1.Enabling and Disabling Multiple Selections in a JTree Component
14.71.2.Allow only a single node to be selected (default)Allow only a single node to be selected (default)
14.71.3.Allow selection to span one vertical contiguous set of visible nodes
14.71.4.Allow multiple selections of visible nodes
14.71.5.Listening for Selection Events in a JTree Component
14.71.6.Getting the Selected Nodes in a JTree Component
14.71.7.Returns a TreePath containing the specified node.
14.71.8.Determine if the third item is selected