Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SWT CheckBox Button get checked/unchecked state

Tags:

java

checkbox

swt

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.
        }
    }
});
like image 984
saurabh Avatar asked Oct 30 '14 14:10

saurabh


1 Answers

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());
        }
    });
like image 116
alex2410 Avatar answered Sep 28 '22 04:09

alex2410