Get tree path from TreeNode : TreeModel « Swing « Java Tutorial






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

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

public class Utils {
  public static TreePath getPath(TreeNode treeNode) {
    List<Object> nodes = new ArrayList<Object>();
    if (treeNode != null) {
      nodes.add(treeNode);
      treeNode = treeNode.getParent();
      while (treeNode != null) {
        nodes.add(0, treeNode);
        treeNode = treeNode.getParent();
      }
    }

    return nodes.isEmpty() ? null : new TreePath(nodes.toArray());
  }
}








14.68.TreeModel
14.68.1.Tree node editing with DefaultTreeModel
14.68.2.implements TreeModelimplements TreeModel
14.68.3.Genealogy Tree
14.68.4.JTree.getModel().addTreeModelListener(new TreeModelListener())
14.68.5.Expand All for a tree path
14.68.6.Get tree path from TreeNode