Quillion
Quillion

Reputation: 6476

Swt resize Composite but not the window

I am trying to change the size of the composite. I do it with the following.

GridData gd = (GridData)parent_.getLayoutData();
gd.heightHint = (int)(parent_.getSize().y / 1.73);

So when a certain action is performed I would like to resize that component and make it smaller. However after caling that I have to do getShell().pack(); and whenever I do that what happens is that the whole window where the component is located takes up as much space vertically as there is available on the screen. Any idea how I prevent the window from doing that? I am really bad at swt, and would love some tutorials too if you can point me in the right direction.

Upvotes: 0

Views: 4775

Answers (1)

Baz
Baz

Reputation: 36884

You don't have to call pack() after changing the GridData, calling layout() on the Shell is sufficient:

public static void main(String[] args)
{
    Display display = Display.getDefault();
    final Shell shell = new Shell(display);
    shell.setLayout(new GridLayout(1, true));

    final Composite resizable = new Composite(shell, SWT.BORDER);
    new Label(resizable, SWT.NONE).setText("Content");

    final GridData data = new GridData(SWT.FILL, SWT.BEGINNING, true, true);
    data.heightHint = 200;

    resizable.setLayoutData(data);

    Button button = new Button(shell, SWT.PUSH);
    button.setText("Resize");
    button.addListener(SWT.Selection, new Listener()
    {
        @Override
        public void handleEvent(Event arg0)
        {
            data.heightHint /= 2;
            shell.layout();
        }
    });

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

Before pressing the button:

enter image description here

After pressing the button:

enter image description here


As for the tutorials:

Upvotes: 4

Related Questions