Here you can find the source of expandPath(JTree tree, TreePath tp)
static public void expandPath(JTree tree, TreePath tp)
//package com.java2s; import javax.swing.JTree; import javax.swing.tree.TreeModel; import javax.swing.tree.TreePath; public class Main { static public void expandPath(JTree tree, TreePath tp) { Object root = tree.getModel().getRoot(); expandPath(tree, new TreePath(root), tp, 0); }/*from w ww . j ava 2 s.co m*/ static public void expandPath(JTree tree, TreePath targetPath, TreePath tp, int pos) { Object[] nodes = tp.getPath(); Object node = targetPath.getLastPathComponent(); Object tpNode = nodes[pos]; if (node.equals(tpNode)) { tree.expandPath(targetPath); } else { return; } TreeModel model = tree.getModel(); if (pos < nodes.length - 1) { int n = model.getChildCount(node); for (int i = 0; i < n; i++) { Object child = model.getChild(node, i); if (child.equals(nodes[pos + 1])) { expandPath(tree, targetPath.pathByAddingChild(child), tp, pos + 1); } } } } }