jkteater
jkteater

Reputation: 1391

createButton - SWT.PUSH versus OK

This is my createButtonsForButtonsBar Method.

protected void createButtonsForButtonBar(Composite parent) {
  Button okButton = createButton(parent, SWT.PUSH, "Close Aplot", true);
  okButton.setEnabled(true);
  okButton.addSelectionListener(new SelectionAdapter() {
     public void widgetSelected(SelectionEvent e) {
        viewer = null;
        getShell().setVisible(false);
    }
  });
}

I had to change

Button okButton = createButton(parent, OK, "Close Aplot", true);

to

Button okButton = createButton(parent, SWT.PUSH, "Close Aplot", true);

Because the OK version was throwing a runtime null pointer error at this line

getShell().setVisible(false);

Error roughly:

Unhandled event loop exception java.lang.NullPointerException at com.test.BaseDialog$7.widgetSelected(BaseDialog.java:277

Question :

What is the difference between OK and SWT.PUSH?

Why would OK cause the error above?

Upvotes: 0

Views: 471

Answers (2)

ctangudu
ctangudu

Reputation: 84

Whatever Edward mentioned is right. You are should pass button ID in that method. But, the button style is already SWT.PUSH style in org.eclipse.jface.dialogs.Dialog.createButton(Composite, int, String, boolean). Please double check. Also you should call/override org.eclipse.jface.dialogs.Dialog.okPressed() handle close operation for the dialog. Hope this helps as well.

Upvotes: 0

Edward Thomson
Edward Thomson

Reputation: 78673

Because OK is a system button ID that indicates certain behavior. In particular, pressing the OK button will close the dialog. Meanwhile, you had added a button selection listener that also tried to close the dialog. Understandably, this would fail.

What you have done here, however, is to create a button that is not a system default button and has no associated default behavior. But you've confused SWT.PUSH - a style constant - with a button ID. If you want to provide your own button ID, you need to use an ID greater than IDialogConstants.CLIENT_ID.

What you probably want to do here is to simply let the system close your dialog for you, removing your selection listener entirely. If there's something you want to do when your dialog is closing - say cleaning up some handles - you should simply override close().

Upvotes: 2

Related Questions