これは、 を使用する際の一般的なハードルScrolledComposite
です。スクロール バーを表示する必要があるほど小さくなった場合、クライアント コントロールを水平方向に縮小して、スクロール バー用のスペースを確保する必要があります。これには、一部のラベルが行を折り返すという副作用があり、次のコントロールがさらに下に移動し、コンテンツ コンポジットに必要な最小の高さが増加しました。
コンテンツ コンポジット ( ) の幅の変更をリッスンしmParent
、新しいコンテンツの幅を指定して最小の高さを再度計算setMinHeight()
し、スクロールされたコンポジットを新しい高さで呼び出す必要があります。
public void createPartControl(Composite parent) {
parent.setLayout(new FillLayout());
ScrolledComposite scrollBox = new ScrolledComposite(parent, SWT.V_SCROLL);
scrollBox.setExpandHorizontal(true);
scrollBox.setExpandVertical(true);
// Using 0 here ensures the horizontal scroll bar will never appear. If
// you want the horizontal bar to appear at some threshold (say 100
// pixels) then send that value instead.
scrollBox.setMinWidth(0);
mParent = new Composite(scrollBox, SWT.NONE);
FormLayout layout = new FormLayout();
mParent.setLayout(layout);
// Adds a bunch of controls here
mParent.addListener(SWT.Resize, new Listener() {
int width = -1;
public void handleEvent(Event e) {
int newWidth = mParent.getSize().x;
if (newWidth != width) {
scrollBox.setMinHeight(mParent.computeSize(newWidth, SWT.DEFAULT).y);
width = newWidth;
}
}
}
// Wait until here to set content pane. This way the resize listener will
// fire when the scrolled composite first resizes mParent, which in turn
// computes the minimum height and calls setMinHeight()
scrollBox.setContent(mParent);
}
サイズの変更をリッスンする際に、幅が同じままであるサイズ変更イベントを無視することに注意してください。これは、幅が同じである限り、コンテンツの高さを変更してもコンテンツの最小高さに影響しないためです。