私は解決策を見つけることができました。
鍵は2つの異なるものでした:
- コンテンツ (CHILDREN が追加され、layout() が呼び出された
ScrolledComposite
場合) と (子の外からサイズ変更された場合) の両方にサイズ変更リスナーを設定してください。
GridData.grab
と の両方を必ず設定してくださいGridData.hint
。ヒントは、コンポジットがこのサイズを想定していることを確認しますがcomputeSize()
、グラブは利用可能な余分なスペースを確実に取得します。
コードサンプルは次のとおりです。
public static void main (String [] args) {
Display display = new Display ();
Shell shell = new Shell(display);
ScrolledComposite sc = new ScrolledComposite(shell, SWT.NONE);
Composite foo = new Composite(sc, SWT.NONE);
foo.setLayout(new GridLayout(1, false));
StyledText text = new StyledText(foo, SWT.NONE);
text.setText("Ipsum dolor etc... \n etc... \n etc....");
GridDataFactory.fillDefaults().grab(true, true).hint(40, 40).applyTo(text);
Listener l = new Listener() {
public void handleEvent(Event e) {
Point size = sc.getSize();
Point cUnrestrainedSize = content.computeSize(SWT.DEFAULT, SWT.DEFAULT);
if(size.y >= cUnrestrainedSize.y && size.x >= cUnrestrainedSize.x) {
content.setSize(size);
return;
}
// does not fit
Rectangle hostRect = getBounds();
int border = getBorderWidth();
hostRect.width -= 2*border;
hostRect.width -= getVerticalBar().getSize().x;
hostRect.height -= 2*border;
hostRect.height -= getHorizontalBar().getSize().y;
c.setSize(
Math.max(cUnrestrainedSize.x, hostRect.width),
Math.max(cUnrestrainedSize.y, hostRect.height)
);
}
}
sc.addListener(SWT.Resize, l);
foo.addListener(SWT.Resize, l);
shell.open ();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}