SWT:使用ScrolledComposite的嵌套布局超出可用空间

时间:2022-10-10 12:03:59

I want to build a Master-Detail layout for one of my applications using SWT.

我想使用SWT为我的一个应用程序构建一个Master-Detail布局。

Container, Content, Sidebar and Part1 are Composite instances. Scrolled is a ScrolledComposite

Container,Content,Sidebar和Part1是Composite实例。滚动是ScrolledComposite

The desired layout is something like:

所需的布局是这样的:

+--Container-------------------------------------+
|+--Content----------------------++--Sidebar----+|
||                               ||+--Part1----+||
||                               |||           |||
||                               |||           |||
||                               |||           |||
||                               |||           |||
||                               |||           |||
||                               ||+-----------+||
||                               ||+--Scrolled-+||
||                               |||           |||
||                               |||           |||
||                               |||           |||
||                               |||           |||
||                               |||           |||
||                               ||+-----------+||
|+-------------------------------++-------------+|
+------------------------------------------------+

The Content should grab all the space horizontally and vertically that is available.

内容应该可以水平和垂直地抓取所有空间。

The sidebar is basically a container for Part1 and Scrolled which should be of equal height.

侧边栏基本上是Part1和Scrolled的容器,它应该具有相同的高度。

Scrolled is the container for a Composite containing a dynamic number of sub items which are arranged in the content composite. Because there can be a large variation of the number of sub-items, this Composite should be scrollable.

Scrolled是Composite的容器,其中包含在内容合成中排列的动态数量的子项。因为子项的数量可能有很大的变化,所以这个Composite应该是可滚动的。

I have now implemented this in the following way:

我现在用以下方式实现了这个:

  • Container has a GridLayout with 2 colums.

    Container有一个GridLayout,有2个列。

  • Inside of that the content has FILL_BOTH behaviour and grabs also all HORIZONTAL/VERTICAL space.

    在内部,内容具有FILL_BOTH行为并且还获取所有HORIZONTAL / VERTICAL空间。

  • The sidebar has a FillLayout(SWT.VERTICAL) and contains the Part1 and Scrolled Childs.

    侧边栏有一个FillLayout(SWT.VERTICAL),包含Part1和Scrolled Childs。

Problem: When putting a lot of items into the scrolled composite and the layout just overflows the available space and there is no scrolling available where it should be.

问题:当将大量项目放入滚动的复合材料中时,布局只会溢出可用空间,并且没有可用的滚动位置。

When I use a FillLayout(SWT.HORIZONTAL) for the container the behaviour is as desired, since there is a scrolling and everything is "inside bounds".

当我对容器使用FillLayout(SWT.HORIZONTAL)时,行为是所希望的,因为滚动并且一切都是“内部边界”。

Is there a way to achieve this behaviour also when using a GridLayout because I want the content to grab most of the space.

有没有办法在使用GridLayout时实现此行为,因为我希望内容占用大部分空间。

Attached the current test SWT snippet:

附上当前测试的SWT片段:

public class Scrolled {
    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setLayout(new GridLayout(2,false));
        //shell.setLayout(new FillLayout(SWT.HORIZONTAL));

        Composite content = new Composite(shell, SWT.BORDER);
        content.setLayoutData(new GridData(SWT.LEFT,SWT.CENTER,false,false));

        Composite sidebar = new Composite(shell, SWT.BORDER);
        sidebar.setLayout(new FillLayout(SWT.VERTICAL));
        sidebar.setLayoutData(new GridData(SWT.LEFT,SWT.CENTER,false,false));

        Composite cc = new Composite(sidebar, SWT.BORDER);

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

        Composite c = new Composite(sc, SWT.NONE);
        c.setSize(400, 400);
        c.setLayout(new GridLayout(1, true));

        for(int i = 0; i < 1000; i++){
            new Button(c, SWT.PUSH).setText("Text");
        }

        sc.setMinSize(c.computeSize(SWT.DEFAULT, SWT.DEFAULT));
        sc.setContent(c);
        sc.setExpandHorizontal(true);
        sc.setExpandVertical(true);
        sc.setAlwaysShowScrollBars(true);

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

1 个解决方案

#1


seems like I found the solution.

好像我找到了解决方案。

Seems to be the case that a Composite has to take GRAB_EXCESS_VERTICAL to be limited to available width. Otherwise the component gets assigned the preferred height which is the maximum height to display all at once.

似乎复合材料必须将GRAB_EXCESS_VERTICAL限制为可用宽度。否则,组件将被分配首选高度,这是一次显示所有内容的最大高度。

The following code changes did the trick:

以下代码更改起了作用:

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

sidebar.setLayoutData(new GridData(SWT.FILL,SWT.FILL,false,true));

#1


seems like I found the solution.

好像我找到了解决方案。

Seems to be the case that a Composite has to take GRAB_EXCESS_VERTICAL to be limited to available width. Otherwise the component gets assigned the preferred height which is the maximum height to display all at once.

似乎复合材料必须将GRAB_EXCESS_VERTICAL限制为可用宽度。否则,组件将被分配首选高度,这是一次显示所有内容的最大高度。

The following code changes did the trick:

以下代码更改起了作用:

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

sidebar.setLayoutData(new GridData(SWT.FILL,SWT.FILL,false,true));