jtree 默认展开树的全部节点

xiaoxiao2021-02-28  94

参考:http://kwon.iteye.com/blog/749643

    public static void expandTree(JTree tree) {          TreeNode root = (TreeNode) tree.getModel().getRoot();          expandAll(tree, new TreePath(root), true);      }          private static void expandAll(JTree tree, TreePath parent, boolean expand) {          // Traverse children          TreeNode node = (TreeNode) parent.getLastPathComponent();          if (node.getChildCount() >= 0) {              for (Enumeration e = node.children(); e.hasMoreElements(); ) {                  TreeNode n = (TreeNode) e.nextElement();                  TreePath path = parent.pathByAddingChild(n);                  expandAll(tree, path, expand);              }          }          // Expansion or collapse must be done bottom-up          if (expand) {              tree.expandPath(parent);          } else {              tree.collapsePath(parent);          }      } 
转载请注明原文地址: https://www.6miu.com/read-49589.html

最新回复(0)