读书人

java。Swing中Tree怎么设置默认所有子

发布时间: 2013-06-25 23:45:42 作者: rapoo

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

读书人网 >Java相关

热点推荐