我有一个包含以下代码的对话框。出现滚动条,但直到最后才滚动。最后的内容无法显示太长时间。即使将对话框的大小调整为较小,也看不到末尾的内容。请提出在以下情况下如何正确使用滚动条的建议。
public class SampleDialog extends TrayDialog {
public SampleDialog(final Shell shell) {
super(shell);
this.shell = shell;
}
@Override
public void create() {
super.create();
}
@Override
protected Control createDialogArea(final Composite parent) {
final GridLayout layout = new GridLayout();
layout.numColumns = 1;
parent.setLayout(layout);
final ScrolledComposite sc1 = new ScrolledComposite(parent, SWT.V_SCROLL | SWT.BORDER);
sc1.setExpandHorizontal(true);
sc1.setExpandVertical(true);
//sc1.setMinWidth(0);
final Composite composite = new Composite(sc1, SWT.NONE);
final GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 2;
composite.setLayout(gridLayout);
sc1.setContent(composite);
//this.sc1.setMinSize(parent.computeSize(SWT.DEFAULT, SWT.DEFAULT));
//this.sc1.setMinHeight(50);
//this.sc1.setAlwaysShowScrollBars(true);
sc1.addControlListener(new ControlAdapter() {
@Override
public void controlResized(final ControlEvent e) {
final Rectangle r = sc1.getClientArea();
sc1.setMinSize(parent.computeSize(r.width, SWT.DEFAULT));
}
});
final GridData gridDataLabel = new GridData();
final Label label = new Label(composite, SWT.NONE);
label.setText("Testing");
label.setLayoutData(gridDataLabel);
for (i = 0 ; i <= n ; i++) { // n can be 1 to any number -- decided at run time
createSampleCombo(composite);
}
}
public void createSampleCombo(Composite parent) {
final Composite composite = new Composite(parent, SWT.NONE);
final GridLayout layout = new GridLayout();
layout.numColumns = 3;
composite.setLayout(layout);
final GridData gridDataCombo = new GridData();
gridDataCombo.horizontalAlignment = GridData.FILL;
gridDataCombo.grabExcessHorizontalSpace = true;
final Combo myCombo = new Combo(composite, SWT.NONE);
myCombo.setLayoutData(gridDataCombo);
gridDataCombo.minimumWidth = 500;
final GridData gridDataButton = new GridData();
final Button browse = new Button(composite, SWT.PUSH);
browse.setText("Browse");
browse.setLayoutData(gridDataButton);
}
}
where:
org.eclipse.jface.dialogs.TrayDialog;
org.eclipse.swt.layout.GridLayout;
在第三个图像中,最后一个控件不可见,并且“确定”和“取消”按钮也不可见。我需要滚动条才能显示所有内容。请建议如何做到这一点
最佳答案
我最近有一个类似的问题,我是这样解决的:首先,应防止ScrolledComposite垂直扩展:
sc1.setExpandVertical( false );
如果然后将resizeListener更改为:
Rectangle clientArea = scrolledComposite.getClientArea();
Point minSize = scrolledComposite.getContent().computeSize( clientArea.width, SWT.DEFAULT );
scrolledComposite.getContent().setSize( minSize );
ScrolledComposite应该只占用其容器布局允许的空间。
有关如何使用ScrolledComposites的更多信息,请参见:
http://www.codeaffine.com/2016/03/01/swt-scrolledcomposite/