简体   繁体   English

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

[英]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 . 我有一个复选框按钮,我想将变量设置为truefalse 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: 要验证选择,请使用事件的getSource()方法获取对象( Button )并检查是否已选中:

    checkBox.addSelectionListener(new SelectionAdapter() {

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从复选框获取未经检查和检查的值 - get unchecked and checked values from a checkbox 选中或取消选中后立即获取复选框状态 - Get checkbox status as soon as it is checked or unchecked Android-如何在更改活动后保存复选框状态(已选中/未选中) - Android - How to save checkbox state (checked/unchecked) after changing activity 处理已选中和未选中的复选框 - Handle checked and unchecked checkbox 如何基于选中或未选中复选框来禁用apex命令按钮? - How to disable apex command button based on a checkbox being checked or unchecked? 如何在不单击表单的提交按钮的情况下每次选中和取消选中复选框时在数据库中插入值 - How to insert value in Database every time when checkbox is checked and unchecked without clicking submit button of form 跟踪每次选中和未选中复选框的情况 - Keeping Track of Every Time that a Checkbox is checked and unchecked 为Checkbox for Android更改已选中和未选中的图标 - Change icons of checked and unchecked for Checkbox for Android 如何检查Android应用程序中的复选框是否已选中? - How to check if checkbox is checked or unchecked in android application? 选中复选框并按下按钮时如何保存其状态 - How to save the state of the checkbox when it is checked and a button pressed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM