java - 如何在选择每个子项时设置根复选框被选中(GWT)

标签 java oop checkbox gwt

我正在构建 GWT 应用程序,其中有带有复选框的 Tree 和 TreeItems。我有一个名为 allCheckBox 的根 CheckBox 及其子元素 rootCheckBox(此复选框也有它们的子元素,但这对此无关紧要)。我希望,当用户打开带有复选框的对话框时,如果选择了每个 childCheckBox,则选择此复选框。我已经做到了,当选择根复选框时,子复选框也会被选择。
这是我的一段代码:

enter cod  GWT_DOMAIN_SERVICE.findAll(new AsyncCallback<List<GwtDomain>>() {

        @Override
        public void onFailure(Throwable caught) {
            exitMessage = MSGS.dialogAddPermissionErrorDomains(caught.getLocalizedMessage());
            exitStatus = false;
            hide();
        }

        @Override
        public void onSuccess(List<GwtDomain> result) {

            for (final GwtDomain gwtDomain : result) {

                GWT_DOMAIN_SERVICE.findActionsByDomainName(gwtDomain.name(), new AsyncCallback<List<GwtAction>>() {

                    @Override
                    public void onFailure(Throwable caught) {
                        exitMessage = MSGS.dialogAddPermissionErrorActions(caught.getLocalizedMessage());
                        exitStatus = false;
                        hide();
                    }

                    @Override
                    public void onSuccess(List<GwtAction> result) {
                        checkedItems = new GwtCheckedItems();
                        checkedItems.setName(gwtDomain);

                        rootCheckBox = new CheckBox();
                        rootCheckBox.setBoxLabel(gwtDomain.toString());
                        listCheckBoxes.add(rootCheckBox);
                        rootTreeItem = new TreeItem(rootCheckBox);
                        childCheckBoxMapList = new HashMap<GwtAction, CheckBox>();
                        checkedItems.setMap(childCheckBoxMapList);
                        for (GwtAccessPermission gwtAccessPermission : checkedPermissionsList) {
                            if (gwtAccessPermission.getPermissionDomain().toString().equals(checkedItems.getName().toString())) {
                                if (gwtAccessPermission.getPermissionAction().toString().equals(GwtAction.ALL.toString())) {
                                    rootCheckBox.setValue(true);
                                }
                            }
                        }
                        if (listOfNewClass.size() == checkedPermissionsList.size()) {
                            allCheckBox.setValue(true);
                        }
                        for (final GwtAction gwtAction : result) {

                            final CheckBox childTreeItemCheckox = new CheckBox();
                            treeItem = new TreeItem(childTreeItemCheckox);
                            childTreeItemCheckox.setBoxLabel(gwtAction.toString());

                            rootTreeItem.addItem(treeItem);

                            childListOfNewClass.add(gwtAction);
                            allTreeItem.addItem(rootTreeItem);
                            childCheckBoxMapList.put(gwtAction, childTreeItemCheckox);
                            for (GwtAccessPermission gwtAccessPermission : checkedPermissionsList) {
                                if (gwtAccessPermission.getPermissionDomain().toString().equals(gwtDomain.toString())) {

                                    if (gwtAccessPermission.getPermissionAction().toString().equals(gwtAction.toString())) {
                                        childTreeItemCheckox.setValue(true);
                                    }
                                }
                            }
                        }

                        listOfNewClass.put(checkedItems, rootCheckBox);

                    }

                });

            }

            allCheckBox.addListener(Events.OnClick, new Listener<BaseEvent>() {

                @Override
                public void handleEvent(BaseEvent be) {
                    if (allCheckBox.getValue()) {
                        for (CheckBox checkBox : listCheckBoxes) {
                            if (!checkBox.getValue()) {
                                checkBox.setValue(true);
                            }
                        }
                    } else {
                        for (CheckBox checkBox : listCheckBoxes) {
                            checkBox.setValue(false);
                        }
                    }
                }
            });

如何设置当所有 rootCheckBox 都被选中时,allCheckBox 也被选中?

编辑:这个checkedPermissionsList是被选中的rootCheckBox的列表。

最佳答案

监听器必须迭代子框并查看它们是否全部被选中,并相应地设置父框

Listener<BaseEvent> listener = new Listener<>() {
    public void handleEvent(BaseEvent be) {
        boolean allSet = listCheckBoxes.stream().allMatch(CheckBox::getValue);
        allCheckBox.setValue(allSet); // this will also unselect if appropriate
    }
 }

所有框的监听器都是相同的,因此将其添加到每个框

listCheckBoxes.forEach(box -> box.addListener(Event.OnClick, listener));

在 Java 8 之前的版本中:

Listener<BaseEvent> listener = new Listener<>() {
public void handleEvent(BaseEvent be) {
    boolean allSet = true;
    for (CheckBox child : listCheckBoxes) { 
        if (!child.getValue()) {
            allSet = false; // found a non-checked box
            break;
        }
    }
    allCheckBox.setValue(allSet); // this will also unselect if appropriate
}

// and set the listener to the children with
for (CheckBox box : listCheckBoxes) {
   box.addListener(Event.Clicked, listener);
}

关于java - 如何在选择每个子项时设置根复选框被选中(GWT),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46583433/

相关文章:

php - 从 A 类的 B 类(扩展 A 类)调用静态方法

C++ 使用非常量变量模板初始化对象

node.js - Node Express Jade - 复选框 boolean 值

java - hibernate 组织. hibernate .LazyInitializationException : failed to lazily initialize a collection of role:

java - 从 liferay portlet 下载 zip 压缩的 xml 文件文件夹

java - 如何将 JPanel 添加到 JFrame 的中心?

java - 无法使用 jdbc 瘦驱动程序连接到 oracle 数据库

python - 请帮助我理解 python 类与实例变量

javascript - 无法从 jQuery 创建的表中删除行

javascript - 根据复选框值删除 HTML 字段