wojtek88
wojtek88

Reputation: 299

SWT layout without spacing that allows horizontal filling for given widget

I try to create view that would have 3 columns but without spacing. What's more I need to have central column that have width determined by the overall width of the container (so it grabs excess horizontal space).

GridLayout allows me to create 3 columns width central filled in container, but I can't make spacing between cells in this layout equal to zero. On the other hand in FillLayout it is very easy to have specing equal to zero, but it is impossible to make central column width determined by container width.

Could You tell me how to achieve my goal with SWT Layouts?

Upvotes: 0

Views: 1816

Answers (1)

Baz
Baz

Reputation: 36904

You can set the verticalSpacing and horizontalSpacing of GridLayout to 0 to remove the spacing:

public static void main(String[] args)
{
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("StackOverflow");
    GridLayout layout = new GridLayout(3, false);
    layout.horizontalSpacing = 0;
    layout.verticalSpacing = 0;
    shell.setLayout(layout);

    createContent(new Composite(shell, SWT.BORDER), false);
    createContent(new Composite(shell, SWT.BORDER), true);
    createContent(new Composite(shell, SWT.BORDER), false);

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

private static void createContent(Composite parent, boolean hFill)
{
    if (hFill)
    {
        parent.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        parent.setLayout(new GridLayout(3, true));
    }
    else
    {
        parent.setLayoutData(new GridData(SWT.BEGINNING, SWT.FILL, false, true));
        parent.setLayout(new GridLayout(1, true));
    }

    for (int i = 0; i < 6; i++)
    {
        new Button(parent, SWT.PUSH).setText("Button " + i);
    }
}

That's what it looks like (note the borders next to each other without spacing):

enter image description here

Upvotes: 8

Related Questions