首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >具有GridLayout的ScrolledComposite父级

具有GridLayout的ScrolledComposite父级
EN

Stack Overflow用户
提问于 2012-07-05 16:23:17
回答 3查看 6.4K关注 0票数 8

我想要一个父母带GridLayoutScrolledComposite,但除非我使用FillLayout,否则滚动条不会显示。我对FillLayout的问题是,它的子代占用了相同的可用空间。

在我的例子中有两个小部件,上面的一个应该占用不超过窗口的1/4,而ScrolledComposite应该占用剩余的空间。然而,他们两个都拿走了一半。

有没有办法将GridLayoutScrolledComposite一起使用,或者是否可以修改FillLayout的行为

下面是我的代码:

代码语言:javascript
复制
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);

}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-02-22 15:41:59

除了setLayout()之外,还需要调用setLayoutData()。在下面的代码示例中,看看GridData对象是如何构造的,并分别传递给两个setLayoutData()调用。

代码语言:javascript
复制
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, 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);
}
票数 4
EN

Stack Overflow用户

发布于 2016-04-07 00:07:11

NB!这个答案是基于Eclipse RAP的,它的行为可能与常规的SWT有所不同。

几天前我也在纠结于同样的问题。我在同一个页面上有两个ScrolledComposite,我需要左边的那个不会占用比所需空间更多的空间(即使空间是可用的)。

在尝试不同的解决方案时,我注意到ScrolledComposite的行为取决于它的LayoutData,如下所示:

  • 如果layoutData设置为new GridData(SWT.LEFT, SWT.TOP, false, true),则无论父Composite大小发生变化,ScrolledComposite都将保持其预期大小。
  • 如果layoutData设置为new GridData(SWT.LEFT, SWT.TOP, true, true),则ScrolledComposite将根据父级Composite的大小变化进行收缩/扩展。这还包括扩展到所需的更大宽度(意味着列保持相等)。

基于此行为,我能够通过向父Composite添加一个调整大小侦听器来解决这个问题,该侦听器根据父ScrolledComposite更改左侧Composite sizelayoutData

下面的示例说明了这种方法:

代码语言:javascript
复制
public class LayoutingScrolledComposites extends AbstractEntryPoint {
    public void createContents( Composite parent ) {    
        parent.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        parent.setLayout(new GridLayout(2, false));

        ScrolledComposite sc1 = new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL);
        Composite c1 = new Composite(sc1, SWT.BORDER);
        sc1.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, true, true));
        c1.setLayout(new GridLayout(3, false));
        sc1.setContent(c1);
        Label l1 = new Label (c1, SWT.BORDER);
        l1.setText("Some text");
        l1 = new Label (c1, SWT.BORDER);
        l1.setText("Some text");
        l1 = new Label (c1, SWT.BORDER);
        l1.setText("Some text");
        c1.setSize(c1.computeSize(SWT.DEFAULT, SWT.DEFAULT));

        ScrolledComposite sc2 = new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL);
        sc2.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, true, true));
        Composite c2 = new Composite(sc1, SWT.BORDER);
        c2.setLayout(new GridLayout(3, false));
        sc2.setContent(c2);
        Label l2 = new Label (c2, SWT.BORDER);
        l2.setText("Some text");
        l2 = new Label (c2, SWT.BORDER);
        l2.setText("Some text");
        l2 = new Label (c2, SWT.BORDER);
        l2.setText("Some text");
        c2.setSize(c2.computeSize(SWT.DEFAULT, SWT.DEFAULT));

        parent.addListener(SWT.Resize, new Listener() {
            public void handleEvent(Event e) {
                int sc1_x = sc1.getContent().getSize().x;
                int sc2_x = sc2.getContent().getSize().x;

                //Enable/Disable grabExcessHorizontalSpace based on whether both sc's would fit in the shell
                if (LayoutingScrolledComposites.this.getShell().getSize().x > sc1_x+sc2_x) {
                    if (((GridData)sc1.getLayoutData()).grabExcessHorizontalSpace) {
                        //sc1 does not change width in this mode
                        ((GridData)sc1.getLayoutData()).grabExcessHorizontalSpace=false;                        
                    }
                } else {
                    if (!((GridData)sc1.getLayoutData()).grabExcessHorizontalSpace) {
                        //sc1 changes width in this mode
                        ((GridData)sc1.getLayoutData()).grabExcessHorizontalSpace=true;                     
                    }
                }
                parent.layout(); //Needed so that the layout change would take effect during the same event
            }
        });
    }
}

然而,在我看来,这种方法确实有点“黑客”式的解决方案。因此,我希望看到一种更好的方法。

票数 2
EN

Stack Overflow用户

发布于 2012-07-05 18:32:48

我认为您在这里缺少的是为孩子定义GridData

布局控制子对象的位置和大小。并且每个布局类具有对应的布局数据类,该数据类允许配置布局内的每个特定的子节点、它们是否填满了整个空间、它们占用了多少单元格等。

我猜你的网格布局可能有4行,顶部的小部件只接受一个单元格,而另一个孩子接受其余的(3)。这是通过GridData.verticalSpan属性实现的。

看看Understanding Layouts in SWT,并尝试不同的布局数据属性,看看它们有什么作用。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11340453

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档