Monikka
Monikka

Reputation: 528

How to get the scrollbars to work in a ScrolledComposite when using GridLayout?

I am trying to have a scrolled composite with a fixed size content and a label below the scrolled composite display status information of the scrollable content. As I want the controls to resize with the parent, I have used GridLayout on their parent. But I am facing an issue with the scroll bars of the scrolled composite and positioning of the label control. i.e, the scrolling doesn't work unless I set the scrolledcomposite's grid data attribute as

grabExcessVerticalspace = true

If I allow the scrolledcomposite to grab the excess vertical space, the label does not appear below the scrolledcomposite due to the space between them.

public class Snippet5 {

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

    // this button is always 400 x 400. Scrollbars appear if the window is
    // resized to be too small to show part of the button
    ScrolledComposite c1 = new ScrolledComposite(shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
    Button b1 = new Button(c1, SWT.PUSH);
    b1.setText("fixed size button");
    b1.setSize(400, 400);
    c1.setAlwaysShowScrollBars(true);
    c1.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, true, true));
    c1.setContent(b1);

    Label l = new Label(shell, SWT.BORDER);
    l.setText("button clicked");
    GridData layoutData = new GridData(SWT.LEFT, SWT.TOP, true, false);
    layoutData.heightHint = 30;
    layoutData.widthHint  = 400;
    l.setLayoutData(layoutData);

    shell.setSize(600, 300);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}

Any help in having the label appear exactly below the scrolled composite (trim the space) and have the scrolling work is appreciated.

Edit:

Expected behavior:

  1. Extra vertical space to be grabbed by the label.
  2. The scrolledcomposite should enable vertical scrolling when the content cannot be viewed completely when the shell is re-sized.

enter image description here

Upvotes: 0

Views: 523

Answers (1)

Rüdiger Herrmann
Rüdiger Herrmann

Reputation: 20985

You need to set the layout data of the ScrolledComposite to FILL like so:

new GridData( SWT.FILL, SWT.FILL, true, true );

This way, the ScrolledComposite will take the available space of the grid cell.

Upvotes: 0

Related Questions