Java – use the scrolledcomposite parent of GridLayout

I want a scrolledcomposite whose parent grid has GridLayout, but the scroll bar is not displayed unless I use filllayout My problem with filllayout is that its child nodes occupy an equal part of the available space

In my example, there are two widgets. The top widget should not exceed 1 / 4 of the window, and the scrolledcomposite should occupy the remaining space But they account for half

Is there any way to use GridLayout with scrolledcomposite, or can you modify the behavior of filllayout?

This is my code:

private void initContent() {

    //GridLayout shellLayout = new GridLayout();
    //shellLayout.numColumns = 1;
    //shellLayout.verticalSpacing = 10;
    //shell.setLayout(shellLayout);
    shell.setLayout(new FillLayout(SWT.VERTICAL));

    searchComposite = new SearchComposite(shell,SWT.NONE);
    searchComposite.getSearchButton().addListener(SWT.Selection,this);

    ScrolledComposite scroll = new ScrolledComposite(shell,SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER);
    scroll.setLayout(new GridLayout(1,true));

    Composite scrollContent = new Composite(scroll,SWT.NONE);
    scrollContent.setLayout(new GridLayout(1,true));

    for (ChangeDescription description : getChanges(false)) {
        ChangesComposite cc = new ChangesComposite(scrollContent,description);
    }

    scroll.setMinSize(scrollContent.computeSize(SWT.DEFAULT,SWT.DEFAULT));
    scroll.setContent(scrollContent);
    scroll.setExpandVertical(true);
    scroll.setExpandHorizontal(true);
    scroll.setAlwaysShowScrollBars(true);

}

Solution

In addition to setlayout(), you need to call setlayoutdata() In the following code example, see how to construct a griddata object and pass it to each of the two setlayoutdata () calls

private void initContent(Shell shell)
{
    // Configure shell
    shell.setLayout(new GridLayout());

    // Configure standard composite
    Composite standardComposite = new Composite(shell,SWT.NONE);
    standardComposite.setLayoutData(new GridData(SWT.FILL,SWT.TOP,true,false));

    // Configure scrolled composite
    ScrolledComposite scrolledComposite = new ScrolledComposite(shell,SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER);
    scrolledComposite.setLayout(new GridLayout());
    scrolledComposite.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true));
    scrolledComposite.setExpandVertical(true);
    scrolledComposite.setExpandHorizontal(true);
    scrolledComposite.setAlwaysShowScrollBars(true);

    // Add content to scrolled composite
    Composite scrolledContent = new Composite(scrolledComposite,SWT.NONE);
    scrolledContent.setLayout(new GridLayout());
    scrolledComposite.setContent(scrolledContent);
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>