user1464251
user1464251

Reputation: 333

org.eclipse.swt.widgets.Group, can this class actually be scrolled

The class org.eclipse.swt.widgets.Group inherits from Composite which inherits from Scrollable. However, when I call the inherited method getVerticalScrollBar(), it always returns null.

Is it possible to make a Group scrollable? If it is possible, how?

BTW, the Group contains a number of org.eclipse.swt.widgets.Button.Button styled as SWT.CHECK.

Upvotes: 3

Views: 1907

Answers (2)

Baz
Baz

Reputation: 36884

I don't think you can get the Group scrollable itself. However, I managed to achieve the same effect by adding a ScrolledComposite to the Group:

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

Afterwards you can add your Buttons to the Composite innerComp. It might be necessary to use setMinHeight(int height) and setMinWidth(int width) on the ScrolledComposite.

EDIT

Here is a small working example. If you decrease the size of the window, you will see, that the scrollbars will appear:

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

Upvotes: 1

Radu Murzea
Radu Murzea

Reputation: 10900

The Javadoc of SWT says:

Returns the receiver's vertical scroll bar if it has one, and null if it does not.

If the user makes the window/dialog smaller, then the scroll-bar appears. Only then will the methods getVerticalScrollBar() and getHorizontalScrollBar() return a non-null value. You must always check for null when it comes to this. It's just the way SWT is built.

Edit: Try calling the getVerticalScrollBar() method on a Composite or on a Shell instead.

Upvotes: 0

Related Questions