开发者

SWT performance of lots of Composites inside a ScrolledComposite

Below is a modified snippet from eclipse site to show what I'm talking about. There are 2 performance issues.

1) Initially, it takes a number of seconds to display. If I increase the loop to 10000 it takes a long time (probably linear).

2) When you click a button it adds a widget to a panel and then has to lay out all the widgets in the scroller again. And this takes a number of seconds (too long).

Is there some way to accomplish these things I'm trying to do faster?

I'm trying to figure a way to show arbitrary items in a "list" type of view. My understanding is that the standard list widget shows strings only, so I assume I have to set up my own like this. Is there some other way?

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();
    }
}


You can try to achieve the same using a single column Table together with a TableEditor to achieve the same. Take a look at this snippet that shows how to add another widget to a Table cell.

This way you will avoid the creating as many Composites and layout overhead. Also you can use a virtual Table to create the resources only as needed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜