当我打电话给我时,我似乎是
JTree.expandPath(路径)
默认情况下,它的所有父母也都会被扩展。 但我真正想要做的是,设置特定的隐形孩子,以便提前扩展。因此,当扩展节点时,其完整的子树会弹出。
我发现JTree expandedState.put(path, Boolean.TRUE);
内部记录了扩展节点,但我无法访问它。 (p.s.我不想使用反射:)。
为扩展事件安装侦听器会导致许多运行时更新被触发。这就是为什么我更愿意让JTree记录扩展状态。
希望还有其他方法可以做到这一点 任何帮助表示赞赏?
一个讨厌的drawBack..it取决于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);
}
}
/* 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;
}
答案 0 :(得分:1)
您可以更轻松地致电
addTreeExpansionListener(TreeExpansionListener tel)
当调用TreeExpansionListener中的treeExpanded(TreeExpansionEvent event)
方法时,您可以检查是否应该递归扩展新扩展的TreePath的所有子项。
答案 1 :(得分:1)
一种简单的方法是子类JTree并覆盖方法
setExpandedState(TreePath path, boolean state)
在此方法中,不要展开所有父项,而只展开要扩展的子路径。 这正是该方法受到保护的原因:)