添加大量内容时 ScrolledComposite 渲染不正确

ScrolledComposite rendering incorrectly when adding a large amount of content

当我尝试在某个数字 (1638) 之后将大量标签添加到包含在 ScrolledComposite 中的 Composite 时,它​​似乎只是放弃并停止在它之后绘制组件。这是对可以显示的一些东西的硬性限制还是我做错了什么。

如果我只添加一个包含 2000 行文本的标签,也会发生这种情况。

public class LoadsOfLabelsTestDialog extends Dialog
{
    List<Label> displaylabels = new ArrayList<>();
    Composite content, list;
    ScrolledComposite scroll;

    public LoadsOfLabelsTestDialog(Shell parentShell)
    {
        super(parentShell);
    }

    @Override
    protected void configureShell(Shell shell)
    {
        super.configureShell(shell);
        shell.setSize(new Point(700, 500));
        shell.setText("FML"); //$NON-NLS-1$
    }

    @Override
    public Control createDialogArea(final Composite comp)
    {
        content = (Composite) super.createDialogArea(comp);
        content.setLayout(new GridLayout(1, false));
        content.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

        Button set1 = new Button(content, SWT.PUSH);
        set1.setText("Display List 1");
        set1.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                List<String> rows = new ArrayList<>();
                for (int i = 0; i < 2000; i++) {
                    rows.add(i +" row");
                }
                updateList(rows);
            }
        });




        scroll = new ScrolledComposite(content, SWT.V_SCROLL);
        list = new Composite(scroll, SWT.NONE);
        list.setLayout(new GridLayout(1, true));

        scroll.setContent(list);
        scroll.setExpandHorizontal(true);
        scroll.setExpandVertical(true);

        scroll.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, true));
        new Label(content, SWT.HORIZONTAL | SWT.SEPARATOR);
        setScrollSize();
        return content;
    }

    private void setScrollSize() {
        scroll.setMinSize(list.computeSize(SWT.DEFAULT, SWT.DEFAULT));
    }

    private void updateList(List<String> rows) {
        if (this.displaylabels == null) {
            this.displaylabels = new ArrayList<>();
        }
        for (Label l : displaylabels) {
            l.dispose();
        }
        this.displaylabels.clear();


        for (String item : rows) {
            addListLabel(item);
        }
        content.layout(true, true);
        setScrollSize();
    }

    private void addListLabel(String whoText) {
        Label a = new Label(list, SWT.NONE);
        a.setText(whoText);
        this.displaylabels.add(a);
    }

    public static void main(String[] args)
    {
        Display d = new Display();
        Shell s = new Shell();

        LoadsOfLabelsTestDialog fml = new LoadsOfLabelsTestDialog(s);
        fml.open();
    }

}

您达到了硬性限制,可能是控件的最大尺寸。虽然此限制在其他平台上可能略有不同,但您不能任意调整控件的大小。

@greg-449 所建议,更喜欢使用 Table。如果每 table 行的内容不仅仅是图像和文本,您可以添加一个绘制侦听器来自己绘制行内容。