While True
While True

Reputation: 423

SWT Layout in a manually bounded Composite

I have the following code:

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);

    Composite parent = new Composite(shell, SWT.NONE);
    parent.setBounds(20, 20, 400, 300);//since shell doesn't have a layout
    parent.setLayout(new FillLayout(SWT.HORIZONTAL|SWT.VERTICAL));

    Composite child = new Composite(parent, SWT.BORDER);
    child.setLayout(new GridLayout(2, true));

    Label l = new Label(child, SWT.NONE);
    l.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
    l.setText("BLOB-BLOB-BLOB!");

    Button button = new Button(child, SWT.PUSH);
    button.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
    button.setText("TEEEEXTTTT");


    shell.setSize(500, 500);
    shell.open();

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

So, I thought I would have a parent composite bounded as (20, 20, 400, 300) since shell doesn't have a layout and a child composite filling a parent because it has a FillLayout. But I get only a parent placed right. And I have to either set layout for shell or set every bounds for every Control inspite of having layouts being set. Why layouts doesn't work in this case?

Upvotes: 0

Views: 130

Answers (1)

Rüdiger Herrmann
Rüdiger Herrmann

Reputation: 21025

If you force a layout on the shell before it is opened, everything appears as expected:

shell.setSize( 500, 500 );
shell.layout( true, true ); // force layout
shell.open();

I am not sure as to why the layout() call is necessary. But I don't usually use absolute positions either...

As a side note: the FillLayout takes either SWT.HORIZONTAL or SWT.VERTICAL. Specifying both does not make sense.

Upvotes: 1

Related Questions