在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. ssh远程登录出现Host key verification failed.解决办法

    今天通过ssh和域名连接主机: IcarusdeMacBook-Pro:~ icarus$ ssh root@icarusyu.me 出现了如下错误: @@@@@@@@@@@@@@@@@@@@@@@@ ...

  2. linux只读文件系统

    一般方法如下 首先试下重新挂载行不行 mount -o remount,rw /dev/sda3 不行的话用fsck,具体方法如下 1. mount命令查看变成只读文件的位置,比如/dev/sda32 ...

  3. node - 使用request发送http请求

    在nodejs的开发中,有时需要后台去调用其他服务器的接口,这个时候,就需要发送HTTP请求了.有一个简单的工具可以用,Simplified HTTP request client,可以比较方便的模拟 ...

  4. 整理的开源项目(全c#)

    NPOI:读写office办公软件(不需要安装office软件) http://npoi.codeplex.com/downloads/get/70099 消息中间件:DotNetMQ http:// ...

  5. discuz 数据库文件密码修改

    网站系统需要修改的位置有两处 Discuz 和 UC-center ①路径:/wwwroot/config/config_global.php 这个根据你网站安装的路径而定. 打开 config_gl ...

  6. Linux之SElinux安全上下文件(1)

    SELinux:Secure Enhanced Linux,是美国国家安全局(NSA=The National Security Agency)和SCC(Secure Computing Courpo ...

  7. Java队列——Disruptor 的使用

    .什么是 Disruptor  从功能上来看,Disruptor 是实现了“队列”的功能,而且是一个有界队列.那么它的应用场景自然就是“生产者-消费者”模型的应用场合了. 可以拿 JDK 的 Bloc ...

  8. 利用nginx解决跨域问题

    访问我的博客 前言 最近遇到了跨域问题,结合之前[微信支付开发本地接收异步通知回调]的经验,利用 Nginx 实现了跨域. 公司之前为了解决跨域问题,用的是 iFrame,反正对于只做后端的我而言,觉 ...

  9. Spring总结 1.装配bean

    本随笔内容要点如下: 依赖注入 Spring装配bean的方式 条件化装配 一.依赖注入 我理解的依赖注入是这样的:所谓的依赖,就是对象所依赖的其他对象.Spring提供了一个bean容器,它负责创建 ...

  10. Python模块:日志输出—logging模块

    1. logging介绍 Python的logging模块提供了通用的日志系统,可以方便第三方模块或者是应用使用.这个模块提供不同的日志级别,并可以采用不同的方式记录日志,比如文件,HTTP GET/ ...