在我看来,当我打电话时
JTree.expandPath( 路径 )
默认情况下,它的所有父级也会展开。但我真正想做的是,预先设置特定的隐形孩子进行扩展。这样当一个节点展开时,它的完整子树就会弹出。
我发现在 JTree 内部 expandedState.put(path, Boolean.TRUE);
记录了扩展的节点,但我无法访问它。(ps我不想使用反射:)
为扩展事件安装监听器会导致大量运行时更新被触发。这就是为什么我更喜欢让 JTree 记录扩展状态。
希望还有其他方法可以做到。
任何帮助表示赞赏?
接受侦听器解决方案。选项 2
选项 1 覆盖:
一个讨厌的缺点..它取决于 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) {
// 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);
}
}
选项 2 监听器
/* 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;
}