代码之家  ›  专栏  ›  技术社区  ›  Link19

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

  •  0
  • Link19  · 技术社区  · 8 年前

    当我试图在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 回复  |  直到 8 年前
        1
  •  1
  •   Rüdiger Herrmann    8 年前

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

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