SWT removing margin from Composite
Asked Answered
G

2

5

When I embedding Composite control inside ViewPart, a vertical margin/padding is added between textbox and button (button is part of another Composite) , as seen on attached picture.

How can I 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

Garlicky answered 28/5, 2015 at 18:39 Comment(0)
K
10

GridLayout#marginHeight and GridLayout#marginWidth can be set to 0 to remove margins.

In your case setting GridLayout#marginTop of the NewComposite may be sufficient.

JavaDoc of 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();
}

Looks like this:

enter image description here

If you want to reduce the spacing between the composites further, you can set the GridLayout#verticalSpacing to 0 as well:

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

Which will look like this:

enter image description here

Kenley answered 28/5, 2015 at 18:54 Comment(4)
Setting layout.marginHeight = 0; layout.marginTop = 0; inside NewComposite didn't helpGarlicky
it's not working in my example, and even in your example there is a gap between 2 buttonsGarlicky
@Garlicky Yes, because the first button still has the marginKenley
In my example, layout.verticalSpacing = 0; solved the issue! 10xGarlicky
L
0

GridLayout#horizontalSpacing and GridLayout#verticalSpacing can be set to 0 to remove spacing between column and row.

In your case setting GridLayout#verticalSpacing of the parent composite should solved the problem.

Libya answered 15/11, 2016 at 6:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.