Java – check the checkbox selection in the listener

At present, we are working hard, but Jface has encountered a small problem

This 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");

Thank you for your help

Edit what I want to do is to create a menu. Some people must accept the terms and conditions to go beyond a point. The default is blank, but when the box is selected, the next button will appear. If not, the next button will remain inactive

Solution

Just make the button final and access it from the 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 button from 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);
    }
});
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>