gpt4 book ai didi

java - 仅当在 Swing 中选择了所有子节点时才选择父节点

转载 作者:行者123 更新时间:2023-12-01 11:18:21 25 4
gpt4 key购买 nike

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

Java Swing: Need a good quality developed JTree with checkboxes

但是当我单击单个子节点时,父节点也会被选中。但我只想在选择其所有子节点时才选择父节点,否则不选择父节点。

最佳答案

只需添加此代码

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

// 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;
}

功能齐全供引用

// 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);
}

关于java - 仅当在 Swing 中选择了所有子节点时才选择父节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31532506/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com