你能帮我解决一些 SWT 列表布局和渲染问题吗
Can you help me with some SWT List layout and rendering issues
我一直在努力修复一个错误,在这个错误中,将大约 2500 个项目添加到 SWT 列表会导致布局中断,我相信这是 SWT 中渲染的错误,因为几乎没有什么可以出错的,但是当您使用滚动组合向下滚动列表,在某个时候,组合中的下一个项目将呈现在列表上,滚动将停止(在 2100 左右)并且列表中的项目消失。如果您注释掉 151 附近的行或我的代码,您可以自己看到这种效果。
但是我已经意识到,如果我向列表的布局数据添加高度提示,它将添加它自己的滚动条,这将解决渲染问题,但引入了一个新问题,那就是我不能' t让List贪婪地占据水平space,让滚动条在面板的右边,有谁知道有什么方法可以让List这样拉伸吗?
import java.util.Collection;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
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;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.swt.widgets.Widget;
public class FilterLayoutDialog extends Dialog {
private ListDisplayer keyWordListDisplay;
private ScrolledComposite scroll;
private Composite parent;
public FilterLayoutDialog(final Shell parentShell) {
super(parentShell);
}
@Override
protected void configureShell(final Shell shell) {
super.configureShell(shell);
shell.setSize(new Point(450, 550));
shell.setText("FML"); //$NON-NLS-1$
}
@Override
public Control createDialogArea(final Composite comp) {
scroll = new ScrolledComposite(comp, SWT.V_SCROLL);
parent = new Composite(scroll, SWT.NONE);
parent.setLayout(new GridLayout(1, true));
scroll.setContent(parent);
scroll.setExpandHorizontal(true);
scroll.setExpandVertical(true);
scroll.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, true));
final ToolBar keywordBar = new ToolBar(parent, SWT.RIGHT | SWT.FLAT);
ToolItem addText = new ToolItem(keywordBar, SWT.RIGHT | SWT.FLAT);
addText.setToolTipText("Add 3000");
addText.setText("Add 3000");
addText.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(final SelectionEvent e) {
keyWordListDisplay
.setContent(IntStream.range(0, 3000).mapToObj(i -> i + "").collect(Collectors.toList()));
parent.layout();
setScrollSize();
}
});
addText = new ToolItem(keywordBar, SWT.RIGHT | SWT.FLAT);
addText.setToolTipText("Add 12");
addText.setText("Add 12");
addText.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(final SelectionEvent e) {
keyWordListDisplay
.setContent(IntStream.range(0, 12).mapToObj(i -> i + "").collect(Collectors.toList()));
parent.layout();
setScrollSize();
}
});
final ToolItem reset = new ToolItem(keywordBar, SWT.RIGHT | SWT.FLAT);
reset.setToolTipText("Reset");
reset.setText("Reset");
reset.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(final SelectionEvent e) {
keyWordListDisplay.setEmpty();
parent.layout();
setScrollSize();
}
});
final GridData barData = new GridData(SWT.LEFT, SWT.CENTER, true, false);
keywordBar.setLayoutData(barData);
Label sep = new Label(parent, SWT.HORIZONTAL | SWT.SEPARATOR);
sep.setLayoutData(GridDataFactory.fillDefaults().grab(true, false).span(1, 1).create());
final Composite keywordList = new Composite(parent, SWT.NONE);
keywordList.setLayout(new GridLayout(1, true));
keyWordListDisplay = new ListDisplayer(keywordList, "None Selected");
sep = new Label(parent, SWT.HORIZONTAL | SWT.SEPARATOR);
sep.setLayoutData(GridDataFactory.fillDefaults().grab(true, false).span(1, 1).create());
setScrollSize();
return scroll;
}
public static void main(final String[] args) {
new Display();
final FilterLayoutDialog fml = new FilterLayoutDialog(new Shell());
fml.open();
}
private void setScrollSize() {
scroll.setMinSize(parent.computeSize(SWT.DEFAULT, SWT.DEFAULT));
}
private class ListDisplayer {
String emptyIndicator;
Composite parent;
Widget w;
public ListDisplayer(final Composite parent, final String emptyIndicator) {
this.parent = parent;
this.emptyIndicator = emptyIndicator;
setEmpty();
}
void setContent(final Collection<String> content) {
disposeWidget();
Composite fill = new Composite(parent, SWT.NONE);
fill.setLayout(new FillLayout());
final GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
// Comment this out
if (content.size() > 20) {
GC gc = new GC(parent);
gd.heightHint = gc.getFontMetrics().getHeight() * 20;
}
// End of comment
fill.setLayoutData(gd);
final org.eclipse.swt.widgets.List box = new org.eclipse.swt.widgets.List(fill, SWT.V_SCROLL | SWT.SINGLE);
box.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(final SelectionEvent e) {
box.setSelection(new int[] {});
}
});
for (final String row : content) {
box.add(row);
}
w = fill;
}
void setEmpty() {
disposeWidget();
final Label a = new Label(parent, SWT.NONE | SWT.WRAP);
a.setText(emptyIndicator);
w = a;
}
void disposeWidget() {
if (w != null) {
w.dispose();
}
}
}
}
您尚未在 keywordList
Composite 上设置布局数据,因此它的布局尽可能小。
keywordList.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
有效。
注意:在调试布局时,将 SWT.BORDER
指定为 Composites 的样式通常会有所帮助,这样您就可以看到它们占用的 space(设置背景颜色)。
我一直在努力修复一个错误,在这个错误中,将大约 2500 个项目添加到 SWT 列表会导致布局中断,我相信这是 SWT 中渲染的错误,因为几乎没有什么可以出错的,但是当您使用滚动组合向下滚动列表,在某个时候,组合中的下一个项目将呈现在列表上,滚动将停止(在 2100 左右)并且列表中的项目消失。如果您注释掉 151 附近的行或我的代码,您可以自己看到这种效果。
但是我已经意识到,如果我向列表的布局数据添加高度提示,它将添加它自己的滚动条,这将解决渲染问题,但引入了一个新问题,那就是我不能' t让List贪婪地占据水平space,让滚动条在面板的右边,有谁知道有什么方法可以让List这样拉伸吗?
import java.util.Collection;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
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;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.swt.widgets.Widget;
public class FilterLayoutDialog extends Dialog {
private ListDisplayer keyWordListDisplay;
private ScrolledComposite scroll;
private Composite parent;
public FilterLayoutDialog(final Shell parentShell) {
super(parentShell);
}
@Override
protected void configureShell(final Shell shell) {
super.configureShell(shell);
shell.setSize(new Point(450, 550));
shell.setText("FML"); //$NON-NLS-1$
}
@Override
public Control createDialogArea(final Composite comp) {
scroll = new ScrolledComposite(comp, SWT.V_SCROLL);
parent = new Composite(scroll, SWT.NONE);
parent.setLayout(new GridLayout(1, true));
scroll.setContent(parent);
scroll.setExpandHorizontal(true);
scroll.setExpandVertical(true);
scroll.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, true));
final ToolBar keywordBar = new ToolBar(parent, SWT.RIGHT | SWT.FLAT);
ToolItem addText = new ToolItem(keywordBar, SWT.RIGHT | SWT.FLAT);
addText.setToolTipText("Add 3000");
addText.setText("Add 3000");
addText.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(final SelectionEvent e) {
keyWordListDisplay
.setContent(IntStream.range(0, 3000).mapToObj(i -> i + "").collect(Collectors.toList()));
parent.layout();
setScrollSize();
}
});
addText = new ToolItem(keywordBar, SWT.RIGHT | SWT.FLAT);
addText.setToolTipText("Add 12");
addText.setText("Add 12");
addText.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(final SelectionEvent e) {
keyWordListDisplay
.setContent(IntStream.range(0, 12).mapToObj(i -> i + "").collect(Collectors.toList()));
parent.layout();
setScrollSize();
}
});
final ToolItem reset = new ToolItem(keywordBar, SWT.RIGHT | SWT.FLAT);
reset.setToolTipText("Reset");
reset.setText("Reset");
reset.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(final SelectionEvent e) {
keyWordListDisplay.setEmpty();
parent.layout();
setScrollSize();
}
});
final GridData barData = new GridData(SWT.LEFT, SWT.CENTER, true, false);
keywordBar.setLayoutData(barData);
Label sep = new Label(parent, SWT.HORIZONTAL | SWT.SEPARATOR);
sep.setLayoutData(GridDataFactory.fillDefaults().grab(true, false).span(1, 1).create());
final Composite keywordList = new Composite(parent, SWT.NONE);
keywordList.setLayout(new GridLayout(1, true));
keyWordListDisplay = new ListDisplayer(keywordList, "None Selected");
sep = new Label(parent, SWT.HORIZONTAL | SWT.SEPARATOR);
sep.setLayoutData(GridDataFactory.fillDefaults().grab(true, false).span(1, 1).create());
setScrollSize();
return scroll;
}
public static void main(final String[] args) {
new Display();
final FilterLayoutDialog fml = new FilterLayoutDialog(new Shell());
fml.open();
}
private void setScrollSize() {
scroll.setMinSize(parent.computeSize(SWT.DEFAULT, SWT.DEFAULT));
}
private class ListDisplayer {
String emptyIndicator;
Composite parent;
Widget w;
public ListDisplayer(final Composite parent, final String emptyIndicator) {
this.parent = parent;
this.emptyIndicator = emptyIndicator;
setEmpty();
}
void setContent(final Collection<String> content) {
disposeWidget();
Composite fill = new Composite(parent, SWT.NONE);
fill.setLayout(new FillLayout());
final GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
// Comment this out
if (content.size() > 20) {
GC gc = new GC(parent);
gd.heightHint = gc.getFontMetrics().getHeight() * 20;
}
// End of comment
fill.setLayoutData(gd);
final org.eclipse.swt.widgets.List box = new org.eclipse.swt.widgets.List(fill, SWT.V_SCROLL | SWT.SINGLE);
box.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(final SelectionEvent e) {
box.setSelection(new int[] {});
}
});
for (final String row : content) {
box.add(row);
}
w = fill;
}
void setEmpty() {
disposeWidget();
final Label a = new Label(parent, SWT.NONE | SWT.WRAP);
a.setText(emptyIndicator);
w = a;
}
void disposeWidget() {
if (w != null) {
w.dispose();
}
}
}
}
您尚未在 keywordList
Composite 上设置布局数据,因此它的布局尽可能小。
keywordList.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
有效。
注意:在调试布局时,将 SWT.BORDER
指定为 Composites 的样式通常会有所帮助,这样您就可以看到它们占用的 space(设置背景颜色)。