SWT:带有 ScrolledComposite 的嵌套布局超出了可用空间

发布于 2024-07-23 06:51:23 字数 3181 浏览 10 评论 0原文

我想使用 SWT 为我的一个应用程序构建主从布局。

Container、Content、Sidebar 和 Part1 是复合实例。 Scrolled 是 ScrolledComposite

所需的布局类似于:

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

内容应占据所有可用的水平和垂直空间。

侧边栏基本上是 Part1 和 Scrolled 的容器,其高度应相等。

Scrolled 是 Composite 的容器,其中包含动态数量的子项目,这些子项目排列在内容组合中。 由于子项的数量可能会有很大变化,因此该 Composite 应该是可滚动的。

我现在通过以下方式实现了这一点:

  • Container 有一个包含 2 列的 GridLayout。

  • 其中的内容具有 FILL_BOTH 行为,并且还占据所有水平/垂直空间。

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

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

当我对容器使用 FillLayout(SWT.HORIZONTAL) 时,行为是理想的,因为存在滚动并且所有内容都在“范围内”。

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

附上当前测试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();
    }
}

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

Container, Content, Sidebar and Part1 are Composite instances.
Scrolled is a 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.

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.

I have now implemented this in the following way:

  • Container has a GridLayout with 2 colums.

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

  • The sidebar has a FillLayout(SWT.VERTICAL) and contains the Part1 and 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".

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

Attached the current test SWT snippet:

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();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

掩耳倾听 2024-07-30 06:51:23

看来我找到了解决方案。

似乎复合材料必须采用 GRAB_EXCESS_VERTICAL 来限制可用宽度。 否则,组件将被分配首选高度,即一次显示的最大高度。

以下代码更改就达到了目的:

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

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

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.

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));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文