JFace 对话框中的 ScrolledComposite 没有滚动条
ScrolledComposite no scrollbar in JFace dialog
我目前正在尝试扩展 JFace
对话框以包含 scrolledComposite
。我的代码目前看起来像这样:
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
public class MWE extends MessageDialog {
public static void main(String[] args) {
Dialog d = new MWE(new Display().getActiveShell());
d.open();
}
public MWE(Shell parentShell) {
super(parentShell, "MWE", null, "",
MessageDialog.ERROR, new String[] { "OK" }, 0);
}
@Override
public Control createDialogArea(Composite parent) {
ScrolledComposite sc = new ScrolledComposite(parent, SWT.V_SCROLL);
sc.setExpandHorizontal(true);
sc.setExpandVertical(true);
sc.setMinSize(300, 300);
Composite content = new Composite(sc, SWT.NONE);
sc.setContent(content);
content.setLayout(new GridLayout());
for (int i = 0; i < 100; i++) {
Label l = new Label(content, SWT.NONE);
l.setText("lorem ipsum");
}
return parent;
}
}
问题是,像这样 window 的大小只是跨越整个屏幕高度,而不是在超过特定大小时可滚动。如果内容超出大小,我该如何滚动?
在 ScrolledComposite
的布局数据中设置一个 heightHint
:
GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
data.heightHint = 500;
sc.setLayoutData(data);
使用计算出的内容大小作为最小大小:
sc.setMinSize(content.computeSize(SWT.DEFAULT, SWT.DEFAULT));
(在所有内容添加到 content
后执行此操作)
我目前正在尝试扩展 JFace
对话框以包含 scrolledComposite
。我的代码目前看起来像这样:
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
public class MWE extends MessageDialog {
public static void main(String[] args) {
Dialog d = new MWE(new Display().getActiveShell());
d.open();
}
public MWE(Shell parentShell) {
super(parentShell, "MWE", null, "",
MessageDialog.ERROR, new String[] { "OK" }, 0);
}
@Override
public Control createDialogArea(Composite parent) {
ScrolledComposite sc = new ScrolledComposite(parent, SWT.V_SCROLL);
sc.setExpandHorizontal(true);
sc.setExpandVertical(true);
sc.setMinSize(300, 300);
Composite content = new Composite(sc, SWT.NONE);
sc.setContent(content);
content.setLayout(new GridLayout());
for (int i = 0; i < 100; i++) {
Label l = new Label(content, SWT.NONE);
l.setText("lorem ipsum");
}
return parent;
}
}
问题是,像这样 window 的大小只是跨越整个屏幕高度,而不是在超过特定大小时可滚动。如果内容超出大小,我该如何滚动?
在 ScrolledComposite
的布局数据中设置一个 heightHint
:
GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
data.heightHint = 500;
sc.setLayoutData(data);
使用计算出的内容大小作为最小大小:
sc.setMinSize(content.computeSize(SWT.DEFAULT, SWT.DEFAULT));
(在所有内容添加到 content
后执行此操作)