SWT table性能改善 -- 使用VirtualTable

在SWT程序中使用table展示数据时,如果数据过多,执行起来会比较慢,不过,我们可以借助VirtualTable来解决这一问题。

Eclipse官网中关于VirtualTable的说明见:http://www.eclipse.org/articles/Article-SWT-Virtual/Virtual-in-SWT.html

 

先来看一个不用VirtualTable的demo:

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

public class SWTTableDemo {
    public static void main(String[] args) {
        long start = System.currentTimeMillis();
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("SWT Table Demo");

        // table cell values
        String[] titles = { "Column1", "Column2", "Column3", "Column4" };
        int items = 20000;
        String[][] cellValues = new String[items][titles.length];
        for (int i = 0; i < items; i++) {
            for (int j = 0; j < titles.length; j++) {
                cellValues[i][j] = "cell_" + (i + 1) + "_" + (j + 1);
            }
        }
        System.out.println("Create data cost:"+ (System.currentTimeMillis() - start));

        Table table = new Table(shell, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
        table.setHeaderVisible(true);

        // set table title
        for (int i = 0; i < titles.length; i++) {
            TableColumn column = new TableColumn(table, SWT.NULL);
            column.setText(titles[i]);
            column.pack();
        }

        for (int loopIndex = 0; loopIndex < items; loopIndex++) {
            TableItem item = new TableItem(table, SWT.NULL);
            item.setText(cellValues[loopIndex]);
        }

        table.setBounds(10, 10, 280, 350);

        shell.pack();
        shell.open();
        long end = System.currentTimeMillis();
        System.out.println("All cost:" + (end - start));

        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
    }
}

上面的代码中,虚构了20000条数据用来填充table,为了显示执行时间,代码中加入了耗时打印~~

执行以下,输出如下:

Create data cost:118
All cost:4783


先不急着评价,来看看VirtualTable的Demo:

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

public class SWTVirtualTableDemo {
    public static void main(String[] args) {
        long start = System.currentTimeMillis();
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("Virtual Table Demo");

        //table cell values
        String[] titles = { "Column1", "Column2", "Column3", "Column4" };
        int items = 20000;
        final String[][] cellValues = new String[items][titles.length];
        for (int i = 0; i < items; i++) {
            for (int j = 0; j < titles.length; j++) {
                cellValues[i][j] = "cell_" + (i + 1) + "_" + (j + 1);
            }
        }
        System.out.println("create data cost:"+(System.currentTimeMillis()-start));

        Table table = new Table(shell, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL | SWT.VIRTUAL);
        table.setHeaderVisible(true);

        // set table title
        for (int i = 0; i < titles.length; i++) {
            TableColumn column = new TableColumn(table, SWT.NULL);
            column.setText(titles[i]);
            column.pack();
        }
        
        table.addListener(SWT.SetData, new Listener(){
            public void handleEvent(Event event) {
                TableItem item = (TableItem)event.item;
                int index = event.index;
                item.setText(cellValues [index]);
            }
        });
        table.setItemCount(items);
        table.setBounds(10, 10, 280, 350);

        shell.pack();
        shell.open();
        long end = System.currentTimeMillis();
        System.out.println("All cost:" + (end - start));
        
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
    }
}

同样是虚构20000条数据用来填充table,执行以下,看看输出结果:

create data cost:118
All cost:181

一个是4783ms,一个是181ms,孰优孰劣不言自明!

 

使用virtual table很简单,就三个要点:

①在创建表时,加上SWT.VIRTUAL属性,eg:

//SWT.VIRTUAL
Table table = new Table(shell, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL | SWT.VIRTUAL);

②给表加上SWT.SetData类型的事件监听,eg:

table.addListener(SWT.SetData, new Listener(){
        public void handleEvent(Event event) {
            TableItem item = (TableItem)event.item;
            int index = event.index;
            item.setText(cellValues [index]);
        }
    });

③设置表中要显示的行数,eg:

table.setItemCount(items);//这里是20000

 

经此三个小步骤,你的表格性能将大幅得到提升

 

 

 

posted @ 2013-11-27 14:43  yejg1212  阅读(3801)  评论(0编辑  收藏  举报