简体   繁体   中英

SWT CheckBox Button get checked/unchecked state

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());
        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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