user2644040
user2644040

Reputation: 115

eclipse swt TitleAreaDialog extra vertical space

Im trying to make a very basic dialog that extends TitleAreaDialog, and it creates this additoinal vertical space that is frustrating, I can't seem to find what is creating this space and prevent it from happening. Below is a screenshot and the code.

https://i.sstatic.net/NoQly.jpg

import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class ImageDialog extends TitleAreaDialog {

public ImageDialog(Shell arg0) {
    super(arg0);
}

@Override
protected void configureShell(Shell shell) {
    super.configureShell(shell);
    Display display = Display.getCurrent();
    Rectangle bounds = display.getPrimaryMonitor().getBounds();
    Rectangle rect = shell.getBounds();

    int x = bounds.x + (bounds.width - rect.width) / 2;
    int y = bounds.y + (bounds.height - rect.height) / 2;

    shell.setLocation(x,y);
}

@Override
protected void setShellStyle(int arg0) {
    super.setShellStyle(SWT.CLOSE| SWT.MODELESS | SWT.BORDER | SWT.TITLE);
    setBlockOnOpen(false);
}

private Text txtLink;
private String link;

@Override
public void create() {
    super.create();
    setTitle("Enter an image url");
    setMessage("Valid formats are .gif, .jp(e)g, .bmp, .png", IMessageProvider.INFORMATION);
}

@Override
protected Control createDialogArea(Composite parent) {
    //Composite area = (Composite) super.createDialogArea(parent);
    Composite container = new Composite(parent, SWT.NONE);
    container.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    GridLayout layout = new GridLayout(2, false);
    //container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    container.setLayout(layout);

    createLinkText(container);
    return parent;
}

private void createLinkText(Composite container) {
    Label lbtLink = new Label(container, SWT.NONE);
    lbtLink.setText("Link: ");

    GridData dataLink = new GridData();
    dataLink.grabExcessHorizontalSpace = true;
    dataLink.horizontalAlignment = GridData.FILL;

    txtLink = new Text(container, SWT.BORDER);
    txtLink.setLayoutData(dataLink);
}

@Override
protected void okPressed() {
    System.out.println(getInitialSize());
    this.link = txtLink.getText();
    super.okPressed();
}

public String getLink() {
    return link;
}
} 

Creating layouts in swt is always a battle for me, any help is greatly appreciated.

Upvotes: 1

Views: 1389

Answers (2)

andi
andi

Reputation: 922

The "offender" can be found in TitleAreaDialog.getInitialSize(). It uses some hard-coded constants for minimum size.

Depending on how you want the dialog to look like, there are several solutions:

  • override getInitialSize() and specify whatever size you want
  • override initializeBounds() and set some size of your own, or call this.getShell().pack(). In this case don't forget to first call super.initializeBounds() because from what I see from the code, it seems it does more than just initializing the bounds.

Upvotes: 1

greg-449
greg-449

Reputation: 111216

You need to use the Composite returned by super.createDialogArea:

@Override
protected Control createDialogArea(Composite parent) {

    Composite area = (Composite) super.createDialogArea(parent);

    Composite container = new Composite(area, SWT.NONE);

    container.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    GridLayout layout = new GridLayout(2, false);
    container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    container.setLayout(layout);

    createLinkText(container);
    return parent;
}

Upvotes: 0

Related Questions