0

I would like to create a dialog without the OK/Cancel buttons. I know that if you override the createButton method, this can be achieved.

What do you think of overriding the createButtonBar method to return null if the button bar is not required at all? This would save some code.

user3726374
  • 583
  • 1
  • 4
  • 24

2 Answers2

5

Overriding createButtonBar is going to produce errors if you return null for the result composite as the Dialog code expects it to not be null.

You can override createButtonsForButtonBar and not create any buttons. It looks like Dialog always checks that individual buttons exist.

You can remove the space used by the buttons composite like this:

@Override
protected void createButtonsForButtonBar(final Composite parent)
{ 
  GridLayout layout = (GridLayout)parent.getLayout();
  layout.marginHeight = 0;
}
greg-449
  • 109,219
  • 232
  • 102
  • 145
  • The only drawback to this is that I loose the window space where the button bar is created. Do you maybe know another way how to completely (and safely) remove the button bar? – user3726374 Aug 21 '14 at 11:02
  • You can adjust the `GridLayout` on the parent composite passed to `createButtonsForButtonBar` to remove the margins. – greg-449 Aug 21 '14 at 11:35
  • Thanks for your answer, this works. Just one more thing: can I be certain that the button bar has a GridLayout, or should I check with instanceof? – user3726374 Aug 21 '14 at 11:52
  • The current `Dialog` code always uses `GridLayout`. Some Eclipse dialogs such as the 'About' dialog also adjust the GridLayout. – greg-449 Aug 21 '14 at 12:02
  • One question: Would it not be better to replace gridLayout.marginHeight=0 by gridData.exclude=true ? – altralaser Jan 24 '16 at 22:06
  • @altralaser That is a possibility, I wouldn't say it was better as it might have unexpected results if you then override `createButtonBar`. – greg-449 Jan 25 '16 at 08:28
0

If you want to have the only one "Close" button on your dialog, you can do it so:

@Override
public void create() {
    super.create();
    getButton(IDialogConstants.OK_ID).setVisible(false);
    getButton(IDialogConstants.CANCEL_ID).setText("Close");
}
Viktor Sirotin
  • 169
  • 1
  • 8