Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to expand JTree nodes (in advance), but keep them invisible
    text
    copied!<p>I seems to me that when i call </p> <blockquote> <p>JTree.expandPath( path )</p> </blockquote> <p>That by default all its parents are expanded as well. But what i actually want to do is, set up specific invisible children to be expanded in advance. So that when a node is expanded that its complete sub-tree pops-out.</p> <p>I've found that internally in JTree <code>expandedState.put(path, Boolean.TRUE);</code> records the expanded nodes, but i can't get access to it. (p.s. i don't want to use reflection :)</p> <p>Installing listener for the expansion event, would result in a lot of run-time updates beeing fired. That's why i prefer to let JTree record the expanded states.</p> <p>Hope that there are other ways of doing it.<br> Any help is appreciated?</p> <h2>Accepted the Listener solution. Option 2</h2> <h3>Option 1 Overriding :</h3> <p>One nasty drawBack..it is depending on the implementation of setExpandedState()</p> <pre><code>private boolean cIsExpandingHidden = false; private TreePath cExpandingPath; public void expandBranchHidden(DefaultMutableTreeNode node) { TreeNode[] mPathSections = mNode.getPath(); TreePath mPath = new TreePath(mPathSections); // if (!cTree.isVisible(mPath)){ cIsExpandingHidden = true; } cExpandingPath = mPath; try{ expandPath(mPath); }finally{ cExpandingPath = null; cIsExpandingHidden = false; } } @Override public boolean isExpanded(TreePath path) { // override the questions whether the node parents of // 'the currently expanded node' to return TRUE if (cIsExpandingHidden){ if (path.isDescendant(cExpandingPath)){ return true; // just claim it doesn't need expanding } } return super.isExpanded(path); } @Override public void fireTreeExpanded(TreePath path) { // the treeUI must not to know.. bad luck for any other listeners if (!cIsExpandingHidden){ super.fireTreeExpanded(path); } } </code></pre> <h3>Option 2 Listener</h3> <pre><code>/* code where new nodes are received */ { // only if direct parent is expanded.. else expansionListener will pick it up if (cTree.isExpanded(mCreator.getParentTreeNode())){ for (TreeNode mNode : mAddNodes) { if (mNode.isDefaultExpanded()) { cTree.expandBranch(mNode); mNode.setDefaultExpanded(false); } } } } /* expansion listener */ { if (cRecursiveExpand){ return; } // walk through children, expand and clear its preference cRecursiveExpand = true; IExtendedTreeNode[] mNodes = cTree.getChildrenOfCurrentNode(); for (IExtendedTreeNode mNode : mNodes) { TreeNode mTreeNode = (TreeNode)mNode; if (mTreeNode.isDefaultExpanded()){ cTree.expandBranch(mTreeNode); mTreeNode.setDefaultExpanded(false); } } cRecursiveExpand = false; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload