有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

添加大量内容时,java ScrolledComposite呈现不正确

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

如果我只添加一个包含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();
    }

}

共 (1) 个答案

  1. # 1 楼答案

    您遇到了一个硬限制,可能是控件的最大大小。虽然此限制在其他平台上可能略有不同,但不能随意调整控件的大小

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