In this case, do not override createButtonsForButtonBar , which has already configured GridLayout so that the buttons are on the right. Instead, override createButtonBar , which allows you to control the entire composition.
Itβs easy to add something on the left side and then let SWT put the OK / Cancel buttons for you β which I recommend, otherwise you would have to duplicate the logic of where the default button is located (far- directly on Mac OS and recent GNOME, to the left of the Cancel button on Win32 and older than GNOME.) In this case, you can configure a composite element that spans the entire button bar, and then let SWT draw the button bar to the far right.
For instance:
@Override protected Control createButtonBar(final Composite parent) { final Composite buttonBar = new Composite(parent, SWT.NONE); final GridLayout layout = new GridLayout(); layout.numColumns = 2; layout.makeColumnsEqualWidth = false; layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING); buttonBar.setLayout(layout); final GridData data = new GridData(SWT.FILL, SWT.BOTTOM, true, false); data.grabExcessHorizontalSpace = true; data.grabExcessVerticalSpace = false; buttonBar.setLayoutData(data); buttonBar.setFont(parent.getFont()); // place a button on the left final Button leftButton = new Button(buttonBar, SWT.PUSH); leftButton.setText("Left!"); final GridData leftButtonData = new GridData(SWT.LEFT, SWT.CENTER, true, true); leftButtonData.grabExcessHorizontalSpace = true; leftButtonData.horizontalIndent = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN); leftButton.setLayoutData(leftButtonData); // add the dialog button bar to the right final Control buttonControl = super.createButtonBar(buttonBar); buttonControl.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, false)); return buttonBar; }
source share