It seems to me that when I call
JTree.expandPath (path)
By default, all his parents are also extended. But what I really want to do is to establish certain invisible children that will be expanded in advance. Thus, when a node expands, its full subtree pops up.
I found that internally expandedState.put(path, Boolean.TRUE);extended nodes are written to JTree , but I cannot access it. (ps I do not want to use reflection :)
Setting a listener for an extension event will trigger a large number of run-time updates. Therefore, I prefer JTree to record advanced states.
Hope there are other ways to do this.
Any help is appreciated?
A decision has been made by the receiver. Option 2
Option 1 Override:
One nasty drawBack .. it depends on the implementation of setExpandedState ()
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) {
if (cIsExpandingHidden){
if (path.isDescendant(cExpandingPath)){
return true;
}
}
return super.isExpanded(path);
}
@Override
public void fireTreeExpanded(TreePath path) {
if (!cIsExpandingHidden){
super.fireTreeExpanded(path);
}
}
Option 2 Listener
{
if (cTree.isExpanded(mCreator.getParentTreeNode())){
for (TreeNode mNode : mAddNodes) {
if (mNode.isDefaultExpanded()) {
cTree.expandBranch(mNode);
mNode.setDefaultExpanded(false);
}
}
}
}
{
if (cRecursiveExpand){
return;
}
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;
}
source
share