日期:2014-05-20 浏览次数:20682 次
for(int i=0; i<tree.getRowCount(); i++)
{
tree.expandRow(i);
}
使用方法:SwingUtil.expandTree(tree);
public class SwingUtil {
public static void expandTree(JTree tree) {
TreeNode root = (TreeNode) tree.getModel().getRoot();
expandTree(tree, new TreePath(root));
}
public static void expandTree(JTree tree, TreePath path) {
TreeNode node = (TreeNode) path.getLastPathComponent();
// Go to leaf
if (node.getChildCount() > 0) {
Enumeration<TreeNode> children = node.children();
while (children.hasMoreElements()) {
TreeNode n = children.nextElement();
TreePath newPath = path.pathByAddingChild(n);
expandTree(tree, newPath);
}
}
tree.expandPath(path);
}
}