0

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

Baz
  • 36,440
  • 11
  • 68
  • 94
Raj Pannala
  • 67
  • 1
  • 9

1 Answers1

2

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.

Baz
  • 36,440
  • 11
  • 68
  • 94