java - 如何创建 GWT 中检查的对象列表?

标签 java checkbox gwt gxt

我正在开发一个使用复选框的 GWT 应用程序。我有复选框中的 GwtRoles 列表,但我不知道如何获取选中的 GwtRoles。这是我的代码:

@Override
    public void createBody() {

    for (GwtRole gwtRole : roleList) {
                checkBox = new CheckBox();
                checkBox.setBoxLabel(gwtRole.getName());
                for (GwtAccessRole gwtAccessRole : lista) {
                    if (gwtRole.getId().equals(gwtAccessRole.getRoleId())) {
                        checkBox.setValue(true);
                    }

RoleList 是复选框中的 GwtRoles 列表。此列表是用户打开表单时应预先检查的项目列表。我不太熟悉 GWT 中的复选框。 我使用了 CheckBox 列表,并且有方法 getChecked() 返回所有已检查的 GwtRoles 的列表,但在这里使用此复选框时我没有该选项。 在此方法中,我应该创建一个已检查的 GwtRoles 列表:

 @Override
    public void submit() {

        List<GwtAccessRoleCreator> listCreator = new ArrayList<GwtAccessRoleCreator>();

        for (GwtRole role : list) {
            GwtAccessRoleCreator gwtAccessRoleCreator = new GwtAccessRoleCreator();

            gwtAccessRoleCreator.setScopeId(currentSession.getSelectedAccount().getId());

            gwtAccessRoleCreator.setAccessInfoId(accessInfoId);

            gwtAccessRoleCreator.setRoleId(role.getId());
            listCreator.add(gwtAccessRoleCreator);
        }
        GWT_ACCESS_ROLE_SERVICE.createCheck(xsrfToken, currentSession.getSelectedAccount().getId(), userId, listCreator, new AsyncCallback<GwtAccessRole>() {

            @Override
            public void onSuccess(GwtAccessRole arg0) {
                exitStatus = true;
                exitMessage = MSGS.dialogAddConfirmation();
                hide();
            }

有人可以帮助我如何设置已检查的 GwtRoles 列表吗?

最佳答案

Map 中跟踪您的 CheckBox,然后仅返回选中复选框的 GwtRole

private Map<GwtRole, CheckBox> mapping = new HashMap<>();

@Override
public void createBody() {
    for (GwtRole gwtRole : roleList) {
        CheckBox checkBox = new CheckBox();
        mapping.put(gwtRole, checkBox);
        // Your other code here.
    }
}

// Java 8
public List<GwtRole> getChecked()
{
    return mapping.entrySet().stream()
        .filter(e -> e.getValue().getValue())
        .map(Map.Entry::getKey)
        .collect(Collectors.toList());
}

// Pre-Java 8
public List<GwtRole> getChecked()
{
    List<GwtRole> result = new ArrayList<>();

    for(Map.Entry<GwtRole, CheckBox> e : map.entrySet())
    {
        if(e.getValue().getValue())
            result.add(e.getKey());
    }

    return result;
}

关于java - 如何创建 GWT 中检查的对象列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45999910/

相关文章:

java - 使用 Apache Beam 将 SAP HANA 连接到 Big Query

php - 如何禁用该复选框并使用 php 更新数据库

GWT-对象化 : basic

java - Cucumber 步骤 - 使用持久性上下文测试 EJB 服务

java - GWT CellTree @UIHandler SelectionChanged

java - 多个复选框。仅选择一项。如何获得它的值

python - 尝试从 ttkwidgets 解析 CheckboxTreeview 并获取树中所有项目的复选框状态

gwt - 如何使 GWT 列表框可以从选择中编辑一部分

java - GWT CELLTABLE SimplePager : Event handling on first, SimplePager 中是否存在上一个、下一个、上一个按钮?

java - SPRING:请求的bean当前正在创建:是否存在无法解析的循环引用