Guardian
Guardian

Reputation: 107

SWT TitleAreaDialog: How to place a Button on the LEFT of ButtonArea?

I just created a new class that extends TitleAreaDialog.

I overrode the method createButtonsForButtonBar and I was able to add new buttons. But placing one on the LEFT?? (Default is the right, besids OK and CANCEL).

Thanks very much!!

Upvotes: 4

Views: 3218

Answers (1)

Edward Thomson
Edward Thomson

Reputation: 78683

In this case, don't override createButtonsForButtonBar, that will have already set up the GridLayout such that the buttons are on the right side. Instead, override createButtonBar, which will let you have control over the entire composite there.

It's easy to add something to the left side and then let SWT place the OK / Cancel buttons for you - which I recommend, since you would otherwise have to duplicate the logic of where the "default" button gets placed (far-right on Mac OS and recent GNOME, to the left of the Cancel button on Win32 and older GNOME.) In this case, you can set up a composite that spans the entirety of the button bar and then let SWT draw its button bar in the far right.

For example:

@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's button bar to the right
    final Control buttonControl = super.createButtonBar(buttonBar);
    buttonControl.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, false));

    return buttonBar;
}

Upvotes: 3

Related Questions