SWT CheckBox按钮获得选中/取消选中状态

时间:2014-10-30 14:59:37

标签: java checkbox swt

我有一个复选框按钮,我想根据该按钮将变量设置为truefalse。但我不知道如何处理这个事件。这是我的代码:

Boolean check = false;
Button checkBox = new Button(composite,SWT.CHECK);
checkBox.setText("CheckBox");
checkBox.addSelectionListener(new SelectionAdapter() {
    @Override
    public void widgetSelected(SelectionEvent event) {
        if (event.detail == SWT.CHECK) {
            // Now what should I do here to get
            // Whether it is a checked event or unchecked event.
        }
    }
});

1 个答案:

答案 0 :(得分:14)

要验证选择,请使用getSource()事件方法获取对象(Button)并检查是否已选中:

    checkBox.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent event) {
            Button btn = (Button) event.getSource();
            System.out.println(btn.getSelection());
        }
    });