Raj Pannala
Raj Pannala

Reputation: 67

Is it possible to add a Button to a ScrolledComposite directly?

ScrolledComposite extends Composite. So is it possible to add Button to the scrolled composite directly without having another composite in it?

Upvotes: 0

Views: 123

Answers (1)

Baz
Baz

Reputation: 36894

Sure it's possible:

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Stackoverflow");
    shell.setLayout(new FillLayout());

    ScrolledComposite sc = new ScrolledComposite(shell, SWT.V_SCROLL | SWT.H_SCROLL);

    Button button = new Button(sc, SWT.NONE);
    button.setText("Hello! This is a button with a lot of text...");

    sc.setContent(button);
    sc.setExpandHorizontal(true);
    sc.setExpandVertical(true);
    sc.setMinSize(button.computeSize(SWT.DEFAULT, SWT.DEFAULT));

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

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

Before resizing:

enter image description here

After resizing:

enter image description here

Just remember to call ScrolledComposite#setContent(Control) with the Button as the parameter.

Upvotes: 2

Related Questions