简体   繁体   English

如何在SWT中实现父子关系复选框?

[英]How to implement parent child relationship checkbox in SWT?

I am new to SWT, my requirement is I need to add two checkboxes in a dialog. 我是SWT的新手,我的要求是我需要在对话框中添加两个复选框。 I want, if I select first checkbox second should also be selected. 我想要,如果我选择了第一个复选框,则还应该选择第二个。 But if I am disabling second checkbox , first one is still selected. 但是,如果我禁用第二个复选框,则第一个仍处于选中状态。 I am not allowed to select second checkbox, second checkbox is only selected when first checkbox is selected. 我不允许选择第二个复选框,仅当选择第一个复选框时才选择第二个复选框。 I believe its a parent child relationship and CheckboxTreeViewer must be used(still not sure). 我相信必须使用其父子关系和CheckboxTreeViewer(仍然不确定)。 Can anyone please send across code snippet for the requirement? 有人可以跨要求发送代码片段吗?

Check out below code: 查看以下代码:

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class CheckBoxExample {
    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setLayout(new GridLayout(3, true));

        Button parentButton = new Button(shell, SWT.CHECK);
        parentButton.setText("Parent");
        Button childButton = new Button(shell, SWT.CHECK);
        childButton.setText("Child");
        childButton.setEnabled(false);

        parentButton.addListener(SWT.Selection, event -> {
            if (!parentButton.getSelection()) {
                childButton.setEnabled(false);
                childButton.setSelection(false);
                return;
            }
            childButton.setEnabled(true);
        });

        shell.pack();
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();
    }
}

Output for above code 以上代码的输出

第1步 第2步 第三步 第四步 步骤5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM