zoukankan      html  css  js  c++  java
  • 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

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

  • 相关阅读:
    VirtualBox Win7 虚拟机 共享文件夹设置
    图像的 SNR 和 PSNR 的计算
    swprintf %s %ws %S 的区别
    解决 Virtual Box 启动 Cannot load R0 module supLoadModule returned VERR_LDR_MISMATCH_NATIVE Failed to register ourselves as a PCI Bus (VERR_MODULE_NOT_FOUND)
    VS2015/2013/2012 IIS Express Debug Classic ASP
    Beginning Android 4 中 Demo Basic/Switch 的问题.
    【虚拟机】解决网络适配器没有 VirtualBox Host-Only Ethernet Adapter 问题
    Docker安装Tomcat镜像并部署web项目
    Docker——入门实战
    Centos7上安装docker
  • 原文地址:https://www.cnblogs.com/yejg1212/p/3445659.html
Copyright © 2011-2022 走看看