7

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

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

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

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Keith Spriggs
  • 235
  • 1
  • 4
  • 10
  • 1
    Your question is not complete. What are you trying to do and what exactly is not working? – Baz Jan 18 '13 at 14:10

1 Answers1

14

Just make the Button final and access it from within 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 the 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);
    }
});
Baz
  • 36,440
  • 11
  • 68
  • 94