在Eclipse視圖
通常情況下,我希望我的控制,以獲取所有可用空間,並且只顯示滾動條,否則控制會低於縮水一個可用的大小。
其他答案是完全有效的,但我想添加一個完整的示例createPartControl
方法(Eclipse e4)。
@PostConstruct
public void createPartControl(Composite parent) {
ScrolledComposite sc = new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL);
Composite composite = new Composite(sc, SWT.NONE);
sc.setContent(composite);
composite.setLayout(new GridLayout(2, false));
Label label = new Label(composite, SWT.NONE);
label.setText("Foo");
Text text = new Text(composite, SWT.BORDER | SWT.WRAP | SWT.V_SCROLL | SWT.MULTI);
GridDataFactory.fillDefaults().grab(true, true).hint(400, 400).applyTo(text);
sc.setExpandHorizontal(true);
sc.setExpandVertical(true);
sc.setMinSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
}
注意.fillDefaults()
意味着.align(SWT.FILL, SWT.FILL)
。
我通常使用這種模式,所以我創建了下面的小幫手方法:
public static ScrolledComposite createScrollable(Composite parent, Consumer<Composite> scrollableContentCreator) {
ScrolledComposite sc = new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
Composite composite = new Composite(sc, SWT.NONE);
sc.setContent(composite);
scrollableContentCreator.accept(composite);
sc.setExpandHorizontal(true);
sc.setExpandVertical(true);
sc.setMinSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
return sc;
}
由於Java的8個lambda表達式,你現在可以在一個非常緊湊的方式實現新的滾動複合材料:
createScrollable(container, composite -> {
composite.setLayout(new FillLayout());
// fill composite with controls
});
非常感謝!現在我學會了在問愚蠢的問題之前總是檢查javadoc ... –