日期:2014-05-20  浏览次数:20656 次

java。Swing中Tree如何设置默认所有子节点展开、、、
求各位大大帮忙了、我是一个新手!java.Swing中Tree如何设置默认所有子节点展开、、、
tree.scrollPathToVisible(new TreePath(node.getPath()));
但是Tree的节点还是不能打开、、
求帮忙了、、、

------解决方案--------------------
遍历所有节点,展开每个
------解决方案--------------------
just do like this
	    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);
    }
}