Quan Nguyen
Quan Nguyen

Reputation: 570

SWT Create Scroll Composite in Group

I want to create a Composite contain 2 Groups. 2 groups have he same width with the composite and can be resize.

Group1 contain a scrollable Composite. This Scrollable Composite has fix height = 400 and contain 20 text fields ==> show V_SCROLL

Group2 contain a text field and height will be dynamic, minimum size is 100 , re-sized by height composite - 400 (height of Scrollable composite)

I try to build this layout but the Group1 always full-fill the composite and I can't see the Group2

Thanks

Upvotes: 0

Views: 6075

Answers (1)

Baz
Baz

Reputation: 36904

You can use GridData#heightHint and GridData#minimumHeight to set your heights. But keep in mind, that enforcing a certain size might not work well on all screen resolutions, because the required size may exceed the available screen size.

However, here is a simple example that should help you:

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

    Group first = new Group(shell, SWT.NONE);
    first.setText("Group 1");
    first.setLayout(new GridLayout(1, false));
    GridData firstData = new GridData(SWT.FILL, SWT.FILL, true, false);
    firstData.heightHint = 400;
    first.setLayoutData(firstData);

    ScrolledComposite firstScroll = new ScrolledComposite(first, SWT.V_SCROLL);
    firstScroll.setLayout(new GridLayout(1, false));
    firstScroll.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    Composite firstContent = new Composite(firstScroll, SWT.NONE);
    firstContent.setLayout(new GridLayout(1, false));
    firstContent.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    for (int i = 0; i < 20; i++)
    {
        Text text = new Text(firstContent, SWT.BORDER);
        text.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));
    }

    firstScroll.setContent(firstContent);
    firstScroll.setExpandHorizontal(true);
    firstScroll.setExpandVertical(true);
    firstScroll.setMinSize(firstContent.computeSize(SWT.DEFAULT, SWT.DEFAULT));

    Group second = new Group(shell, SWT.NONE);
    second.setText("Group 2");
    second.setLayout(new GridLayout(1, false));
    GridData secondData = new GridData(SWT.FILL, SWT.FILL, true, true);
    secondData.minimumHeight = 100;
    second.setLayoutData(secondData);

    Text text = new Text(second, SWT.BORDER | SWT.MULTI);
    text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    shell.pack();
    shell.setSize(400, shell.getSize().y);
    shell.open();

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

Looks like this:

enter image description here

Upvotes: 9

Related Questions