I would like to have a ScrolledComposite
that has multiple composites within it.
If the window is resized, the ScrolledComposite
isn't resizing correctly. If I shrink the width down it will cut the bottom of the content off and not allow the user to scroll down enough. If I then increase the width to much larger it will always keep the scroll bar and will allow the user to scroll too far down.
I've tried updating the minimum height of the ScrolledComposite
like the answer in this question but it hasn't helped. I've tried following multiple tutorials and looking at examples but I can't see what is causing this.
Example
import org.apache.commons.lang3.StringUtils;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
public class Example {
public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("Scrolled Composite Example");
// Create a scrolled composite with content
shell.setLayout(new GridLayout(1, true));
shell.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
final Composite scrollParent = new Composite(shell, SWT.NONE);
scrollParent.setLayout(new FillLayout());
scrollParent.setLayoutData(GridDataFactory.fillDefaults().grab(true, true).span(1, 1).create());
final ScrolledComposite scrollComposite = new ScrolledComposite(scrollParent, SWT.V_SCROLL);
final Composite scrollContent = new Composite(scrollComposite, SWT.NONE);
scrollContent.setLayout(new GridLayout(1, true));
scrollComposite.setContent(scrollContent);
scrollComposite.setExpandHorizontal(true);
scrollComposite.setExpandVertical(true);
// Add a composite to the scroll content (A label with lots of text)
final Composite cell = new Composite(scrollContent, SWT.BORDER);
cell.setLayout(new GridLayout(4, false));
cell.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
final Label l = new Label(cell, SWT.WRAP);
l.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
l.setText(StringUtils.repeat('1', 10000));
// Listen for width changes to update the minimum height
scrollContent.addListener(SWT.Resize, new Listener() {
int width = -1;
@Override
public void handleEvent(final Event e) {
int newWidth = scrollContent.getSize().x;
if (newWidth != width) {
scrollComposite.setMinHeight(scrollContent.computeSize(newWidth, SWT.DEFAULT).y);
width = newWidth;
}
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
What is causing this?