I have a checkbox button based on which I want to set a variable as true
or false
. But I don't know how to handle the event. Here's my code:
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.
}
}
});
To validate selection use getSource()
method of event to get object(Button
) and check is it selected:
checkBox.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent event) {
Button btn = (Button) event.getSource();
System.out.println(btn.getSelection());
}
});