gpt4 book ai didi

java - 你能帮我解决一些 SWT 列表布局和渲染问题吗

转载 作者:行者123 更新时间:2023-12-02 09:08:04 24 4
gpt4 key购买 nike

我一直在努力修复一个错误,即向 SWT 列表添加大约 2500 个项目会导致布局中断,我相信这是 SWT 中渲染的一个错误,因为几乎不会出错,但是当您滚动时使用滚动组合向下滚动列表,在某个时刻,组合中的下一个项目将在列表上呈现,并且滚动将停止(在 2100 左右),列表中的项目就会消失。如果您注释掉 151 附近的行或我的代码,您可以自己看到这种效果。

但是我意识到,如果我向列表的布局数据添加高度提示,它将添加它自己的滚动条,这将解决渲染问题,但引入了一个新问题,那就是我无法解决列表贪婪地占据水平空间,使滚动条位于面板的右侧,有谁知道如何让列表以这种方式拉伸(stretch)?

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 指定为复合样式通常会有所帮助,这样您就可以看到它们占用的空间(o 设置背景颜色)。

关于java - 你能帮我解决一些 SWT 列表布局和渲染问题吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59643946/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com