Lasan
Lasan

Reputation: 183

in java SWT Composite do not lay out according to the set layout

I am expecting to show two composite in the main composite when run the below code.

But it does not show child composites in it unless set the bounds for them. But I am expecting to setBound automatically since I have applied layout. Can some one tell me what is wrong here.

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    Composite composite = new Composite(shell, SWT.BORDER);
    composite.setBounds(0,0,500,500);
    RowLayout rowLayout = new RowLayout();
    rowLayout.type =SWT.VERTICAL;
    rowLayout.fill = true;
    composite.setLayout(rowLayout);

    //
    //
    Composite comB1 = new Composite(composite, SWT.BORDER);

    RowLayout comB1Layout = new RowLayout();
    comB1Layout.type =SWT.HORIZONTAL;
    comB1.setVisible(true);
    //
    //
    Composite comB2 = new Composite(composite, SWT.BORDER);

    RowLayout comB2Layout = new RowLayout();
    comB2Layout.type =SWT.HORIZONTAL;
    comB1.setVisible(true);
    shell.open();


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

}

Upvotes: 0

Views: 186

Answers (1)

greg-449
greg-449

Reputation: 111142

You need to set a layout on the Shell to tell it what to do with the children.

However this will override the setBounds on the Composite. It is difficult to mix layouts with setBounds. Instead call setSize on the Shell.

So something like:

shell.setSize(500, 800);

shell.setLayout(new FillLayout());

Composite composite = new Composite(shell, SWT.BORDER);

RowLayout rowLayout = new RowLayout();
rowLayout.type =SWT.VERTICAL;
rowLayout.fill = true;
composite.setLayout(rowLayout);

Composite comB1 = new Composite(composite, SWT.BORDER);

RowLayout comB1Layout = new RowLayout();
comB1Layout.type =SWT.HORIZONTAL;
comB1.setLayout(comB1Layout);

Composite comB2 = new Composite(composite, SWT.BORDER);

RowLayout comB2Layout = new RowLayout();
comB2Layout.type =SWT.HORIZONTAL;
comB2.setLayout(comB2Layout);

Upvotes: 1

Related Questions