org.eclipse.swt.widgets.Group,这个类实际上可以滚动

时间:2012-08-02 19:21:48

标签: java swt scrollable

班级org.eclipse.swt.widgets.Group继承自Composite继承的Scrollable。但是,当我调用继承的方法getVerticalScrollBar()时,它总是返回null

是否可以使Group可滚动?如果有可能,怎么样?

顺便说一句,Group包含一些org.eclipse.swt.widgets.Button.Button样式为SWT.CHECK

2 个答案:

答案 0 :(得分:1)

我认为你不能自己获得Group的可滚动性。但是,我设法通过向Group添加ScrolledComposite来实现相同的效果:

group = new Group(parent, SWT.NONE);
group.setText("Images: ");
group.setLayout(new FillLayout());

ScrolledComposite comp = new ScrolledComposite(group, SWT.V_SCROLL | SWT.H_SCROLL);
comp.setLayout(new FillLayout());

Composite innerComp = new Composite(comp, SWT.NONE);
innerComp.setLayout(new FillLayout());

comp.setContent(innerComp);
comp.setExpandHorizontal(true);
comp.setExpandVertical(true);

之后,您可以将Button添加到Composite innerComp。可能需要在setMinHeight(int height)上使用setMinWidth(int width)ScrolledComposite

修改

这是一个小工作示例。如果您减小窗口大小,您将看到滚动条将出现:

public static void main(String[] args)
{
    Display display = Display.getDefault();
    Shell shell = new Shell(display);

    shell.setLayout(new FillLayout());

    Group imageGroup = new Group(shell, SWT.V_SCROLL | SWT.H_SCROLL);
    imageGroup.setText("Images: ");
    imageGroup.setLayout(new FillLayout());

    ScrolledComposite frontComp = new ScrolledComposite(imageGroup, SWT.V_SCROLL | SWT.H_SCROLL);
    Composite innerFrontComp = new Composite(frontComp, SWT.NONE);
    frontComp.setLayout(new FillLayout());

    innerFrontComp.setLayout(new FillLayout());

    new Button(innerFrontComp, SWT.CHECK).setText("Button");
    new Button(innerFrontComp, SWT.CHECK).setText("Button");
    new Button(innerFrontComp, SWT.CHECK).setText("Button");
    new Button(innerFrontComp, SWT.CHECK).setText("Button");
    new Button(innerFrontComp, SWT.CHECK).setText("Button");
    new Button(innerFrontComp, SWT.CHECK).setText("Button");
    new Button(innerFrontComp, SWT.CHECK).setText("Button");
    new Button(innerFrontComp, SWT.CHECK).setText("Button");

    frontComp.setMinHeight(innerFrontComp.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
    frontComp.setMinWidth(innerFrontComp.computeSize(SWT.DEFAULT, SWT.DEFAULT).x);
    frontComp.setContent(innerFrontComp);
    frontComp.setExpandHorizontal(true);
    frontComp.setExpandVertical(true);

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

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

答案 1 :(得分:0)

SWT的Javadoc说:

  

返回接收者的垂直滚动条(如果有),如果没有,则返回null。

如果用户使窗口/对话框变小,则会出现滚动条。只有这样,方法getVerticalScrollBar()getHorizontalScrollBar()才会返回非空值。在这方面,您必须始终检查null。这就是SWT的构建方式。

修改:尝试在getVerticalScrollBar()Composite上调用Shell方法。