如何在 Eclipse RCP 应用程序中使视图可滚动?

发布于 2024-12-10 17:07:27 字数 513 浏览 0 评论 0原文

到目前为止我尝试过的:

在 createPartControl:

ScrolledComposite sc = new ScrolledComposite(parent, SWT.V_SCROLL | SWT.H_SCROLL);
        sc.setLayoutData(new GridData(GridData.FILL_BOTH));
        sc.setExpandVertical(true);
        sc.setExpandHorizontal(true);
        sc.setSize(ApplicationWorkbenchWindowAdvisor.WIDTH, ApplicationWorkbenchWindowAdvisor.HEIGHT);
        final TabFolder tabFolder = new TabFolder(sc, SWT.TOP);

但这不起作用。我的问题是,如果我调整程序窗口的大小,滚动条不会出现在我的视图中。有什么想法吗?

What I tried so far:

In createPartControl:

ScrolledComposite sc = new ScrolledComposite(parent, SWT.V_SCROLL | SWT.H_SCROLL);
        sc.setLayoutData(new GridData(GridData.FILL_BOTH));
        sc.setExpandVertical(true);
        sc.setExpandHorizontal(true);
        sc.setSize(ApplicationWorkbenchWindowAdvisor.WIDTH, ApplicationWorkbenchWindowAdvisor.HEIGHT);
        final TabFolder tabFolder = new TabFolder(sc, SWT.TOP);

but this does not work. My problem is that if I resize my program window the scrollbars does not appear in my view. Any ideas?

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

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

发布评论

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

评论(3

看透却不说透 2024-12-17 17:07:27

通常在 Eclipse 视图中,我希望控件能够占据所有可用空间,并且仅显示滚动条,否则控件会缩小到可用大小以下。

其他答案完全有效,但我想添加 createPartControl 方法的完整示例(Eclipse e4)。

@PostConstruct
public void createPartControl(Composite parent) {
    ScrolledComposite sc = new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL);
    Composite composite = new Composite(sc, SWT.NONE);
    sc.setContent(composite);

    composite.setLayout(new GridLayout(2, false));

    Label label = new Label(composite, SWT.NONE);
    label.setText("Foo");

    Text text = new Text(composite, SWT.BORDER | SWT.WRAP | SWT.V_SCROLL | SWT.MULTI);
    GridDataFactory.fillDefaults().grab(true, true).hint(400, 400).applyTo(text);

    sc.setExpandHorizontal(true);
    sc.setExpandVertical(true);
    sc.setMinSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
}

请注意,.fillDefaults() 隐含 .align(SWT.FILL, SWT.FILL)

我通常使用这种模式,因此我创建了以下小辅助方法:

public static ScrolledComposite createScrollable(Composite parent, Consumer<Composite> scrollableContentCreator) {
    ScrolledComposite sc = new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
    Composite composite = new Composite(sc, SWT.NONE);
    sc.setContent(composite);

    scrollableContentCreator.accept(composite);

    sc.setExpandHorizontal(true);
    sc.setExpandVertical(true);
    sc.setMinSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
    return sc;
}

感谢 Java 8 lambda,您现在可以以非常紧凑的方式实现新的可滚动组合:

    createScrollable(container, composite -> {
        composite.setLayout(new FillLayout());
        // fill composite with controls
    });

Typically in Eclipse views, I want my controls to grab all available space, and only show scrollbars, if otherwise a control would shrink below a usable size.

The other answers are perfectly valid, but I wanted to add a full example of a createPartControl method (Eclipse e4).

@PostConstruct
public void createPartControl(Composite parent) {
    ScrolledComposite sc = new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL);
    Composite composite = new Composite(sc, SWT.NONE);
    sc.setContent(composite);

    composite.setLayout(new GridLayout(2, false));

    Label label = new Label(composite, SWT.NONE);
    label.setText("Foo");

    Text text = new Text(composite, SWT.BORDER | SWT.WRAP | SWT.V_SCROLL | SWT.MULTI);
    GridDataFactory.fillDefaults().grab(true, true).hint(400, 400).applyTo(text);

    sc.setExpandHorizontal(true);
    sc.setExpandVertical(true);
    sc.setMinSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
}

Note that .fillDefaults() implies .align(SWT.FILL, SWT.FILL).

I commonly use this pattern, so I created the following little helper method:

public static ScrolledComposite createScrollable(Composite parent, Consumer<Composite> scrollableContentCreator) {
    ScrolledComposite sc = new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
    Composite composite = new Composite(sc, SWT.NONE);
    sc.setContent(composite);

    scrollableContentCreator.accept(composite);

    sc.setExpandHorizontal(true);
    sc.setExpandVertical(true);
    sc.setMinSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
    return sc;
}

Thanks to Java 8 lambdas, you can now implement new scrollable composites in a very compact way:

    createScrollable(container, composite -> {
        composite.setLayout(new FillLayout());
        // fill composite with controls
    });
偷得浮生 2024-12-17 17:07:27

ScrolledComposite的Javadoc描述了两种使用方法,包括示例代码。总结一下:

  1. 您可以在控件/组合本身上设置 ScrolledComposite 中包含的控件/组合的大小
  2. ,或者告诉 ScrolledComposite 用于的最小尺寸它的内容。

目前,你两者都没有做。您正在 ScrolledComposite 上设置大小,但除非您不使用布局管理器,否则这没有多大意义。无论如何,请参阅上面的链接以获取一些官方示例代码。

The Javadoc of ScrolledComposite describes the two ways to use it, including example code. To sum them up:

  1. You either set the size of the control/composite contained in your ScrolledComposite on the control/composite itself
  2. Or you tell your ScrolledComposite the minimum size to use for its content.

Currently, you're doing neither. You're setting the size on the ScrolledComposite, but unless you don't use a layout manager, that doesn't make much sense. In any case, see the above link for some official example code.

何以心动 2024-12-17 17:07:27

这是一小段对我有用的代码:

    ScrolledComposite sc = new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
    Composite composite = new Composite(sc, SWT.NONE);
    sc.setContent(composite);

    Label lblRelation = new Label(composite, SWT.NONE);
    lblRelation.setBounds(10, 13, 74, 15);
    lblRelation.setText("Label name:");
    composite.setSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT));

This is a small piece of code which worked for me:

    ScrolledComposite sc = new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
    Composite composite = new Composite(sc, SWT.NONE);
    sc.setContent(composite);

    Label lblRelation = new Label(composite, SWT.NONE);
    lblRelation.setBounds(10, 13, 74, 15);
    lblRelation.setText("Label name:");
    composite.setSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文