so.very.tired
so.very.tired

Reputation: 3086

Java's SWT: Setting the size of Group hides its widgets

I want to set a Group with some widgets on it in a GridLayout.
I also want the size of the group component to be of fixed size (250x250) and the widgets on it to accommodate (evenly spaced inside it in two columns).
But when I set the size of the Group, it hides its widgets.

Consider this:

public class Gui {

    public static void main(String[] args) {
        Gui g=new Gui();
        g.run();
    }

    private Shell shell;
    private Display display;
    private ChartComposite composite;


    public Gui() {
        display=new Display();
        shell=new Shell(display);
        shell.setSize(500, 500);

        Group group=new Group(shell, SWT.NONE);
        group.setText("Group");
        group.setSize(250,250); // this causes trouble

        GridLayout group_layout=new GridLayout();
        group_layout.numColumns=2;
        group_layout.marginBottom=10;
        group_layout.marginTop=10;
        group.setLayout(group_layout);

        Button b1=new Button(group, SWT.PUSH);
        b1.setText("Button1");

        Button b2=new Button(group, SWT.PUSH);
        b2.setText("Button2");

        Button b3=new Button(group, SWT.PUSH);
        b3.setText("Button3");

        Button b4=new Button(group, SWT.PUSH);
        b4.setText("Button4");
    }

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

The output:

enter image description here

But now watch what happens when I relocate the call to setSize():

public class Gui {

    public static void main(String[] args) {
        Gui g=new Gui();
        g.run();
    }

    private Shell shell;
    private Display display;
    private ChartComposite composite;

    public Gui() {
        display=new Display();
        shell=new Shell(display);
        shell.setSize(500, 500);

        Group group=new Group(shell, SWT.NONE);
        group.setText("Group");

        GridLayout group_layout=new GridLayout();
        group_layout.numColumns=2;
        group_layout.marginBottom=10;
        group_layout.marginTop=10;
        group.setLayout(group_layout);

        Button b1=new Button(group, SWT.PUSH);
        b1.setText("Button1");

        Button b2=new Button(group, SWT.PUSH);
        b2.setText("Button2");

        Button b3=new Button(group, SWT.PUSH);
        b3.setText("Button3");

        Button b4=new Button(group, SWT.PUSH);
        b4.setText("Button4");

        group.setSize(250,250);  // relocated
    }

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

The output:

enter image description here

Does anyone have an explanation for this behaviour?

As I mentioned, I need the size of the Group to be (250x250) before adding the widgets, so they would accommodate appropriately, and I can't get this done if setSize() hides my widgets.

Upvotes: 1

Views: 173

Answers (1)

ralfstx
ralfstx

Reputation: 4023

A layout is computed and applied by a call of the composite's layout() method. In your first snippet, this is never done. Resizing a composite also triggers a re-layout. That's why the call to setSize() in your second snippet triggers the layout. It does not work in your first snippet because the GridLayout is not yet set when you call setSize().

Instead of defering the setSize() call, you could as well call group.layout() after setting the layout and creating all children.

In UIs that use only layouts instead of absolute positioning, a single call to shell.pack() would trigger all layouts recursively.

Upvotes: 1

Related Questions