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
经此三个小步骤,你的表格性能将大幅得到提升
SWT table性能改善 -- 使用VirtualTable的更多相关文章
- JFace TableViewer性能改善 -- 使用VirtualTable
前一篇提到了SWT中的table的通过使用virtual table性能得到很大的改善,那么如果既存的工程中使用的是TableViewer来创建的表,也能改成virtual table吗? 答案是肯定 ...
- 性能改善之For与Foreach
关于For与Foreach的区别,博客园里已经有好多这样文章了,都分析的挺好:http://www.cnblogs.com/jobs/archive/2004/07/17/25218.aspx 不过 ...
- 调整swt table的行高
table.addListener(SWT.MeasureItem, new Listener() { public void handleEvent(Event event) { // 设置行高度 ...
- EF的性能改善和思考
EF是个工具,用的好了性能就会很好,用的不好性能就会有很大损失. 先从EF的设计思想来讲解 EF的初衷是根据缓存中的实体对象,以及实体对象的状态(删除.更新.添加)来对数据库进行操作,这些实体对象.以 ...
- WPF性能改善---之化整为零(蜂窝的衍生应用)
在有的项目中,有这样的需求,由于显示器的显示区域是有限的,而软件却要展示一个远大于显示区域的一些元素,此时就要引入放大.缩小.拖动等UI控制技术,而在使用这些技术的同时,在后台有效的控制渲染元素的个数 ...
- 待性能改善的一个SQL
select t.*, t.rowid from tb_tk_datasakusei_ctrl t; alter table ATOMBB.TB_TK_JISSEKI_INFO_DETAIL add ...
- 性能改善后复杂SQL
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper PUBLIC "-/ ...
- 细数改善WPF应用程序性能的10大方法
WPF(Windows Presentation Foundation)应用程序在没有图形加速设备的机器上运行速度很慢是个公开的秘密,给用户的感觉是它太吃资源了,WPF程序的性能和硬件确实有很大的关系 ...
- [转]响应式WEB设计学习(3)—如何改善移动设备网页的性能
原文地址:http://www.jb51.net/web/70362.html 前言 移动设备由于受到带宽.处理器运算速度的限制,因而对网页的性能有更高的要求.究竟是网页中的何种元素拉低了网页在移动设 ...
随机推荐
- ServiceStack DateTime数据类型转Json出现的困扰
执行dotnet-new selfhost sstest 创建项目,然后打开解决方案 修改ssTest.ServiceModel中的Hello.cs,在HellopResponse中添加时间属性,然后 ...
- Android-----application的学习
一.Application的对象回调函数 1.onCreate : Application对象被创建时候会调用 2.onConfigurationChanged : 屏幕方向变化.系统语言的更改等 3 ...
- 17_python_成员
一.类成员 1.字段 class Province: country = '中国' # 实例 (静态) 字段:类变量. 不属于对象, 对象可以访问 def __init__(self, name): ...
- 如何执行超过一百兆(100MB)的sql脚本?
最近遇到一个问题,在sqlserver的查询分析器里面执行一个超过100MB的数据库脚本,发现老是报“引发类型为“System.OutOfMemoryException”的异常”,上网查了一下,主要是 ...
- svn重新安装后报You need to upgrade the working copy first错误
问题来源 最近重新安装了操作系统,安装了一个最新版的svn,提交代码的时候报了一个错误:You need to upgrade the working copy first,!网上找了很多解决办法,都 ...
- nginx lua集成
版本说明: linux: 1.8.1 luajit:2.0.2(不要使用标准lua,应当使用luajit, 后者的效率比前者高很多) ngx_devel_kit: 0.2.18 lua-nginx-m ...
- Maven内置属性,pom属性
内置属性(Maven预定义,用户可以直接使用) ${basedir}表示项目根目录,即包含pom.xml文件的目录; ${version}表示项目版本; ${project.basedir}同${ba ...
- GO入门——3. 控制语句
1 if 条件表达式没有括号 支持一个初始化表达式(可以是并行方式) 左大括号必须和条件语句或else在同一行 支持单行模式 初始化语句中的变量为block级别,同时隐藏外部同名变量 a := 1 i ...
- linux centos挂载数据盘教程
一.备份/home/liying目录数据前提条件:电脑重启下,保证服务关闭,以免进程影响操作 a.新建backup目录#cd /#mkdir backup b.把/home/liying/目录下的数据 ...
- 浅尝Vue.js组件(一)
本篇目录: 组件名 组件注册 全局注册 基础组件的自动化全局注册 局部注册 在模块系统中局部注册 Prop 单向数据流 Prop验证 类型检查 非Prop特性 替换/合并已有的特性 禁用特性继承 自定 ...