I'm working on to create UI with JAVA SWT.
One thing makes me confused with using ScrolledComposite. Why my code does not show a scroll without setting the MinSize of Composite?
Can anyone explain what setMinSize does in ScrolledComposite?
Why can't we use "setSize" insetead of "setMinSize"?
Below is my code.
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.graphics.Color;
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 MyPractice {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout());
final ScrolledComposite sc = new ScrolledComposite(shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
sc.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
Composite c = new Composite(sc, SWT.NONE);
c.setLayout(new GridLayout(7, true));
c.setBackground(new Color(display, 0,255,0));
for (int i = 0; i < 200; i++) {
Button b = new Button(c, SWT.PUSH);
b.setText("Button " + i);
}
sc.setContent(c);
sc.setExpandHorizontal(true);
sc.setExpandVertical(true);
//*****Why is this code necessary to get Scroll on UI?
sc.setMinSize(c.computeSize(SWT.DEFAULT, SWT.DEFAULT));
//Why can't we use "setSize" insetead of setMinSize? setSize does not show scroll.
//sc.setSize(c.computeSize(SWT.DEFAULT, SWT.DEFAULT));
shell.setSize(300, 500);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
setSize()
andsetMinSize()
are two very different functions.Control#setSize()
sets the actual size of aControl
whereasScrolledComposite#setMinSize()
is specific toScrolledComposite
and tells it at which size to start showing the scroll bars:The minimum size of the scrolled composite will be based on the size of the child.