首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >仅在Swing中选择所有子节点时才选择父节点。

仅在Swing中选择所有子节点时才选择父节点。
EN

Stack Overflow用户
提问于 2015-07-21 07:11:12
回答 1查看 1.4K关注 0票数 0

我正在开发一个IntelliJ插件弹出对话框来检查来自CheckboxTree的值。为此,我将遵循以下问题:

Java Swing: Need a good quality developed JTree with checkboxes

但是,当我单击单个子节点时,父节点也将被选中。但是,只有当父节点的所有子节点都被选中时,才选择父节点,否则将取消选择。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-07-21 08:07:29

只需添加以下代码

updatePredecessorsWithCheckMode函数中注释如下代码,并在for循环后添加代码

代码语言:javascript
复制
// If at least one child is selected, selecting also the parent
//            if (childCheckedNode.isSelected) {
//                parentCheckedNode.isSelected = true;
//            }
        }
   //check the parent if all children are selected
    if (parentCheckedNode.allChildrenSelected) {
        parentCheckedNode.isSelected = true;
    }

全功能可供参考

代码语言:javascript
复制
// When a node is checked/unchecked, updating the states of the predecessors
protected void updatePredecessorsWithCheckMode(TreePath tp, boolean check) {
    TreePath parentPath = tp.getParentPath();
    // If it is the root, stop the recursive calls and return
    if (parentPath == null) {
        return;
    }
    CheckedNode parentCheckedNode = nodesCheckingState.get(parentPath);
    DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) parentPath.getLastPathComponent();
    parentCheckedNode.allChildrenSelected = true;
    parentCheckedNode.isSelected = false;
    for (int i = 0; i < parentNode.getChildCount(); i++) {
        TreePath childPath = parentPath.pathByAddingChild(parentNode.getChildAt(i));
        CheckedNode childCheckedNode = nodesCheckingState.get(childPath);
        // It is enough that even one subtree is not fully selected
        // to determine that the parent is not fully selected
        if (!childCheckedNode.allChildrenSelected) {
            parentCheckedNode.allChildrenSelected = false;
        }
        // If at least one child is selected, selecting also the parent
       // if (childCheckedNode.isSelected) {
        //    parentCheckedNode.isSelected = true;
       // }
    }
   //check the parent if all children are selected
    if (parentCheckedNode.allChildrenSelected) {
        parentCheckedNode.isSelected = true;
    }
    if (parentCheckedNode.isSelected) {
        checkedPaths.add(parentPath);
    } else {
        checkedPaths.remove(parentPath);
    }
    // Go to upper predecessor
    updatePredecessorsWithCheckMode(parentPath, check);
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31532506

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档