当滚动条未显示时,如何摆脱SWT ScrolledComposite中的浪费空间

时间:2013-05-13 07:38:36

标签: java swt

我有一个水平ScrolledComposite拿着两个按钮(实际上是一个复合符 有两个按钮)。即使滚动,也会分配滚动条的空间 酒吧是隐藏的。 (当水平空间足够时,它们会被隐藏起来 滚动是没有必要的。)

enter image description here

如何摆脱这个浪费的空间?

与此同时,我已将滚动复合配置为始终显示 滚动条(setAlwaysShowScrollBars(true)),但我更喜欢得到 摆脱浪费的空间。

enter image description here

感谢您的考虑。

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class ScrollbarSpaceIssue extends Composite
{
    public ScrollbarSpaceIssue(final Composite parent, final int style)
    {
        super(parent, style);

        setLayout(new GridLayout(1, false));

        final ScrolledComposite scrolledComposite = new ScrolledComposite(this, SWT.BORDER | SWT.H_SCROLL);
        scrolledComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
        scrolledComposite.setExpandHorizontal(true);
        scrolledComposite.setExpandVertical(true);
        //scrolledComposite.setAlwaysShowScrollBars(true);
        {
            final Composite composite = new Composite(scrolledComposite, SWT.NONE);
            composite.setLayout(new GridLayout(2, false));
            {
                final Button btn1 = new Button(composite, SWT.NONE);
                btn1.setText("Button 1");

                final Button btn2 = new Button(composite, SWT.NONE);
                btn2.setText("Button 2");
            }

            scrolledComposite.setContent(composite);
            scrolledComposite.setMinSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
        }

        pack();
    }

    public static void main(final String[] args)
    {
        final Display display = new Display();

        final Shell shell = new Shell(display);
        shell.setText("Testcase");
        shell.setLayout(new FillLayout());

        new ScrollbarSpaceIssue(shell, SWT.NONE);

        shell.pack();
        shell.open();

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

1 个答案:

答案 0 :(得分:2)

存在“浪费空间”的原因是ScrolledComposite对象的首选大小包括滚动条的空间,无论它们是否可见。要禁用此默认功能,您可以通过扩展ScrolledComposite来覆盖首选大小计算:

class PackedScrolledComposite extends ScrolledComposite
{
    Point scrollBarSize;  // Size of OS-specific scrollbar

    public PackedScrolledComposite(Composite parent, int style)
    {
        super(parent, style);

        Composite composite = new Composite(parent, SWT.H_SCROLL | SWT.V_SCROLL);
        composite.setSize(1, 1);
        scrollBarSize = composite.computeSize(0, 0);
        composite.dispose();
    }

    public Point computeSize(int wHint, int hHint, boolean changed)
    {
        Point point = super.computeSize(wHint, hHint, changed);
        point.x += ((getStyle() & SWT.V_SCROLL) != 0) ? -scrollBarSize.x : 0;
        point.y += ((getStyle() & SWT.H_SCROLL) != 0) ? -scrollBarSize.y : 0;

        return point;
    }
}