我有一个复选框按钮,我想根据该按钮将变量设置为true
或false
。但我不知道如何处理这个事件。这是我的代码:
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.
}
}
});
答案 0 :(得分:14)
要验证选择,请使用getSource()
事件方法获取对象(Button
)并检查是否已选中:
checkBox.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent event) {
Button btn = (Button) event.getSource();
System.out.println(btn.getSelection());
}
});