在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

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

SWT table性能改善 -- 使用VirtualTable的更多相关文章

  1. JFace TableViewer性能改善 -- 使用VirtualTable

    前一篇提到了SWT中的table的通过使用virtual table性能得到很大的改善,那么如果既存的工程中使用的是TableViewer来创建的表,也能改成virtual table吗? 答案是肯定 ...

  2. 性能改善之For与Foreach

    关于For与Foreach的区别,博客园里已经有好多这样文章了,都分析的挺好:http://www.cnblogs.com/jobs/archive/2004/07/17/25218.aspx  不过 ...

  3. 调整swt table的行高

    table.addListener(SWT.MeasureItem, new Listener() { public void handleEvent(Event event) { // 设置行高度 ...

  4. EF的性能改善和思考

    EF是个工具,用的好了性能就会很好,用的不好性能就会有很大损失. 先从EF的设计思想来讲解 EF的初衷是根据缓存中的实体对象,以及实体对象的状态(删除.更新.添加)来对数据库进行操作,这些实体对象.以 ...

  5. WPF性能改善---之化整为零(蜂窝的衍生应用)

    在有的项目中,有这样的需求,由于显示器的显示区域是有限的,而软件却要展示一个远大于显示区域的一些元素,此时就要引入放大.缩小.拖动等UI控制技术,而在使用这些技术的同时,在后台有效的控制渲染元素的个数 ...

  6. 待性能改善的一个SQL

    select t.*, t.rowid from tb_tk_datasakusei_ctrl t; alter table ATOMBB.TB_TK_JISSEKI_INFO_DETAIL add ...

  7. 性能改善后复杂SQL

    <?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper PUBLIC "-/ ...

  8. 细数改善WPF应用程序性能的10大方法

    WPF(Windows Presentation Foundation)应用程序在没有图形加速设备的机器上运行速度很慢是个公开的秘密,给用户的感觉是它太吃资源了,WPF程序的性能和硬件确实有很大的关系 ...

  9. [转]响应式WEB设计学习(3)—如何改善移动设备网页的性能

    原文地址:http://www.jb51.net/web/70362.html 前言 移动设备由于受到带宽.处理器运算速度的限制,因而对网页的性能有更高的要求.究竟是网页中的何种元素拉低了网页在移动设 ...

随机推荐

  1. 房屋贷款计算器 Mortgage Calculator

    闲暇时间开发了一款工具 - 房屋贷款计算器 Mortgage Calculator 有需要的可以下载来试试. JACK NJ @ 2017

  2. 【LA3485】 Bridge

    前言 哈哈哈,垃圾微积分哈哈哈 前置知识:自适应Simpson法与微积分初步,学会编程 Solution 考虑一下我们有的是什么: 一段桥梁的横向距离,悬线的长度,以及高度. 我们发现如果我们重新设一 ...

  3. Python中super()的用法

    参考链接:https://www.cnblogs.com/shengulong/p/7892266.html super 是用来解决多重继承问题的,直接用类名调用父类方法在使用单继承的时候没问题,但是 ...

  4. 【Anaconda】:科学计算的Python发行版

    [背景] Python易用,但包管理和Python不同版本的问题比较头疼,特别是当你使用Windows的时候.为了解决这些问题,有不少发行版的Python,比如WinPython.Anaconda等, ...

  5. spring mvc开发过程中的乱码问题

    在保证jsp,xml,数据库,编辑器编码一致的情况下. 1,用户输入中文,后台接收到也是中文,但是保存到数据库时乱码, 解决方法: 链接数据库的url="jdbc:mysql://local ...

  6. Alpha冲刺(3/10)——追光的人

    1.队友信息 队员学号 队员博客 221600219 小墨 https://www.cnblogs.com/hengyumo/ 221600240 真·大能猫 https://www.cnblogs. ...

  7. nginx发布静态网页

    http://www.jb51.net/article/71384.htm 切记不要把项目放在/root下 会出现 nginx open() "" failed (13: Perm ...

  8. 使用libcurl的正确姿势

    libcurl支持访问http.ftp等各种服务器,下载图片AV什么的不在话下.但其存在多种接口,异步接口也很难以理解,到底什么样的使用姿势才是正确滴?我们来看看可用的体位: easy interfa ...

  9. webgl之观察三维空间

    在之前的教程中,我们已经接触到了3d的基本应用,而这里,将会继续介绍两种不同的相机,即透视相机和正投影相机:还会学习设置相机的不同参数,这样就可以使场景以不同的角度显示出来. 一.正投影和透视投影概念 ...

  10. python 字符串操作。。

    #字符串操作 以0开始,有负下标的使用0第一个元素,-1最后一个元素,-len第一个元 素,len-1最后一个元素 name= "qwe , erw, qwe "print(nam ...