SWT - How do you re-size/set size of an a Composite residing inside another composite?

Joppe picture Joppe · Dec 13, 2011 · Viewed 10.9k times · Source

//I have a composite within a scrolled composite.

final Composite c = new Composite(scrolledComposite, SWT.NONE); //1
c.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
c.setLayout(new GridLayout(1, false));

//I have a for loop that adds composites to composite c

for(int i = 0; i<100; i++){ 

    Composite b = new Composite(c, SWT.BORDER);
    b.setBackground(SWTResourceManager.getColor(SWT.COLOR_LIST_SELECTION));
    b.setLayout(new GridLayout(2, false));
    b.setBounds(112, 70, 268, 69);

//and then some controllers added to that inner composite

    Label lblNewLabel_2 = new Label(b, SWT.NONE);
    lblNewLabel_2.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 4));
    Image img = new Image(Display.getCurrent(), result.get(i).getPicturePath());


//.... more controllers.

// finally I end the loop and add the outer most composite to an scrolled composite.

} // end loop           
    scrolledComposite.setContent(c); //setter content!
    scrolledComposite.setMinSize(c.computeSize(SWT.DEFAULT, SWT.DEFAULT));

This all works great, almost. My problem is that the inner composite wont respond to the setBounds method. No matter what I'm writing in it I cant get it to expand. I suspect this have something to do with the layouts.

Anyone got a clue?

Thanks in advance.

Petter

Answer

Martti K&#228;&#228;rik picture Martti Käärik · Dec 13, 2011

Layouts are the objects that set size and location of composite's children so you wouldn't have to call setBounds() for every one of them. Check out Understanding Layouts in SWT.

What is the purpose of Layout in SWT applications?