java 从侦听器中选中复选框选择

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14398966/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 16:08:38  来源:igfitidea点击:

Check checkbox selection from within Listener

javabuttonswtjface

提问by Keith Spriggs

Working away at the moment but have come up with a small problem with JFace. I have most of what I need to is have a check box that allows the next button to become active.

目前正在工作,但对 JFace 提出了一个小问题。我需要的大部分内容是有一个复选框,允许下一个按钮变为活动状态。

Here is the code

这是代码

    Button btnConfirm = new Button(container, SWT.CHECK);

    btnConfirm.addSelectionListener(new SelectionAdapter() {
    @Override

    public void widgetSelected(SelectionEvent e) {

          //missing if statement        
          setPageComplete(true);
        }
    });

    btnConfirm.setBounds(330, 225, 75, 20);
    btnConfirm.setText("Confirm");

Thanks in advance for any help

在此先感谢您的帮助

Edit What I'm trying to do is to build a menu where some has to accept the terms and conditions before they can progress beyond a point The default is to blank, but when the box is checked the next button will appear, if it is not then the next button will remain inactive.

Edit What I'm trying to do is to build a menu where some has to accept the terms and conditions before they can progress beyond a point The default is to blank, but when the box is checked the next button will appear, if it is否则下一个按钮将保持不活动状态。

回答by Baz

Just make the Buttonfinaland access it from within the Listener:

只需在 中创建Buttonfinal并访问它Listener

final Button btnConfirm = new Button(shell, SWT.CHECK);

btnConfirm.addSelectionListener(new SelectionAdapter()
{
    @Override
    public void widgetSelected(SelectionEvent e)
    {
        if (btnConfirm.getSelection())
            setPageComplete(true);
        else
            setPageComplete(false);
    }
});


Alternatively, get the Buttonfrom the SelectionEvent:

或者,Button从以下位置获取SelectionEvent

Button btnConfirm = new Button(shell, SWT.CHECK);

btnConfirm.addSelectionListener(new SelectionAdapter()
{
    @Override
    public void widgetSelected(SelectionEvent e)
    {
        Button button = (Button) e.widget;
        if (button.getSelection())
            setPageComplete(true);
        else
            setPageComplete(false);
    }
});