SWT to remove a field from a composite

When I embed the control Compositeinside ViewPart, a vertical field / pad is added between the text box and the button (the button is part of another Composite), as shown in the attached image.

How to remove it?

public class View extends ViewPart {
    public static final String ID = "xyyx.view";

    public void createPartControl(Composite parent) {

        GridLayout layout = new GridLayout(1, false);
        parent.setLayout(layout);


        Text infoText = new Text(parent, SWT.BORDER);

        GridData gridDataInfo = new GridData();
        gridDataInfo.horizontalSpan = 1;
        gridDataInfo.grabExcessHorizontalSpace = true;
        gridDataInfo.horizontalAlignment = GridData.FILL;
        infoText.setLayoutData(gridDataInfo);       

        new NewComposite(parent, SWT.NONE);         

    }

    public class NewComposite extends Composite{

        public NewComposite(Composite parent, int style) {
            super(parent, style);           

            GridLayout layout = new GridLayout(1, false);
            setLayout(layout);      

            Button btn = new Button(parent, SWT.PUSH);
            btn.setText("Button");                  
        }

    }

    @Override
    public void setFocus() {
        // TODO Auto-generated method stub

    }

}

enter image description here

+4
source share
2 answers

GridLayout#marginHeightand GridLayout#marginWidthcan be set to 0to remove fields.

In your case, there may be enough installation GridLayout#marginTopfor NewComposite.

Javoc GridLayout#marginHeight

Like this:

public static void main(String[] args)
{
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new GridLayout(1, false));

    Composite first = new Composite(shell, SWT.NONE);
    Composite second = new Composite(shell, SWT.NONE);

    GridLayout firstLayout = new GridLayout(1, false);
    first.setLayout(firstLayout);

    GridLayout secondLayout = new GridLayout(1, false);
    secondLayout.marginHeight = 0;
    secondLayout.marginWidth = 0;
    second.setLayout(secondLayout);

    Button firstButton = new Button(first, SWT.PUSH);
    firstButton.setText("Margin");
    Button secondButton = new Button(second, SWT.PUSH);
    secondButton.setText("No Margin");

    first.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    second.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    firstButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    secondButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    shell.pack();
    shell.open();

    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
        {
            display.sleep();
        }
    }

    display.dispose();
}

It looks like:

enter image description here

If you want to reduce the distance between the composites, you can also set GridLayout#verticalSpacingto 0:

GridLayout layout = new GridLayout(1, false);
layout.verticalSpacing = 0;
shell.setLayout(layout);

It will look like this:

enter image description here

+7
source

GridLayout # horizontalSpacing GridLayout # verticalSpacing 0, .

GridLayout # verticalSpacing .

0

All Articles