ScrollledComposite中许多复合材料的SWT性能

时间:2011-06-27 19:44:41

标签: java performance user-interface swt

以下是来自eclipse网站的修改后的片段,以显示我正在谈论的内容。有2个性能问题。

1)最初,显示需要几秒钟。如果我将循环增加到10000,则需要很长时间(可能是线性的)。

2)当您单击按钮时,它会向面板添加一个小部件,然后必须再次在卷轴中布置所有小部件。这需要几秒钟(太长时间)。

有没有办法完成这些我想要更快做的事情?

我正试图想方设法在“列表”类型的视图中显示任意项目。我的理解是标准列表小部件只显示字符串,所以我假设我必须像这样设置我自己的。还有其他方法吗?

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.layout.FillLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

public class Snippet188 {
    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setLayout(new FillLayout());
        final ScrolledComposite sc = new ScrolledComposite(shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
        sc.setLayout(new FillLayout());
        final Composite c = new Composite(sc, SWT.NONE);
        RowLayout rl = new RowLayout(SWT.VERTICAL);
        rl.wrap = false;
        c.setLayout(rl);
        System.out.println(System.currentTimeMillis());
        for (int i = 0; i < 1000; i++) {
            final Composite item = new Composite(c, SWT.NONE);
            item.setLayout(new RowLayout(SWT.VERTICAL));
            Label l = new Label(item, SWT.NONE);
            l.setText("Label " + i);
            Button b = new Button(item, SWT.PUSH);
            b.setText("Button " + i);
            final int value = i;
            b.addSelectionListener(new SelectionAdapter() {
                public void widgetSelected(SelectionEvent e) {
                    System.out.println("clicked: " + value);
                    Label newL = new Label(item, SWT.NONE);
                    newL.setText("new Label " + value);
                    item.pack();
                    c.pack();
                }
            });
        }
        System.out.println(System.currentTimeMillis());
        sc.setContent(c);
        sc.setExpandHorizontal(true);
        sc.setExpandVertical(true);
        sc.setMinSize(c.computeSize(SWT.DEFAULT, SWT.DEFAULT));
        sc.setShowFocusedControl(true);

        shell.setSize(300, 500);
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) display.sleep();
        }
        display.dispose();
    }
}

1 个答案:

答案 0 :(得分:2)

您可以尝试使用单个列TableTableEditor来实现相同的目标。请查看此snippet,其中显示了如何将另一个窗口小部件添加到表格单元格。

这样您就可以避免创建尽可能多的复合材料和布局开销。此外,您可以使用虚拟表仅根据需要创建资源。