0

I have a composite with GridLayout. On the first row there is just some text. On the second row there is a button. If i press the button i set the text on the first row to not visible, but everything stays in place. How can i "shrink" the composite so everything that is below the hidden text moves up to take its place?

Here is what i got so far:

public class Main {
    public static void main(String[] args) {
        Display display = Display.getDefault();
        Shell shell = new Shell(display);
        shell.setLayout(new FillLayout());


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

        final Label label = new Label(composite, SWT.BORDER);
        label.setText("Just some text");

        Button b1 = new Button(composite, SWT.PUSH);
        b1.setText("Button1");
        b1.addListener(SWT.Selection, new Listener() {
            public void handleEvent(Event e) {
              switch (e.type) {
              case SWT.Selection:
                  label.setVisible(!label.getVisible());
                break;
              }
            }
          });

        shell.pack();
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();
    }
} 
user1985273
  • 1,817
  • 15
  • 50
  • 85

1 Answers1

1

When you create the label set the layout data:

GridData labelLayoutData = new GridData(SWT.BEGINNING, SWT.CENTER, false, false);
label.setLayoutData(labelLayoutData);

When you change the visibility of the label also set the GridData exclude flag:

label.setVisible(!label.getVisible());

labelLayoutData.exclude = !label.isVisible();

Finally call layout of the Composite

composite.layout(true, true);
greg-449
  • 109,219
  • 232
  • 102
  • 145