Here you can find the source of expandPaths(JTree tree, Collection
static public void expandPaths(JTree tree, Collection<TreePath> paths)
//package com.java2s; import java.util.Collection; import java.util.Iterator; import javax.swing.JTree; import javax.swing.tree.TreeModel; import javax.swing.tree.TreePath; public class Main { static public void expandPaths(JTree tree, Collection<TreePath> paths) { if (paths != null) { for (Iterator<TreePath> it = paths.iterator(); it.hasNext();) { TreePath tp = it.next(); expandPath(tree, tp);//from ww w .jav a 2s . c o m } } } static public void expandPath(JTree tree, TreePath tp) { Object root = tree.getModel().getRoot(); expandPath(tree, new TreePath(root), tp, 0); } 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); } } } } }