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 前言 移动设备由于受到带宽.处理器运算速度的限制,因而对网页的性能有更高的要求.究竟是网页中的何种元素拉低了网页在移动设 ...
随机推荐
- ASP.Net MVC OA项目笔记<二>
1.1.0 创建数据层 1.1.1 CZBK.ItcastOA.IDAL 引用 CZBK.ItcastOA.Model 1.2.1 给IDAL添加一个接口IUserInfoDal 里面写增删改查分页的 ...
- Java Calender 类详解
一. 如何创建 Calendar 对象 Calendar 是一个抽象类, 无法通过直接实例化得到对象. 因此, Calendar 提供了一个方法 getInstance,来获得一个Calendar ...
- 杀掉所有 skynet 进程
ps aux | grep skynet | awk '/config/{print $2}' | xargs kill
- 网易免费企业邮箱Foxmail设置方法
网易免费企业邮箱Foxmail7.0设置方法 第一步:启动 Foxmail 邮件客户端,点击工具->账号管理,弹出如下页面. 点击新建,如下: 填写自己企业邮箱账号,然后下一步,邮箱类型选择PO ...
- Dubbo原理实现之代理接口的定义
Dubbo有很多的实现采用了代码模式,Dubbo由代理工厂ProxyFactory对象创建代理对象. ProxyFactory接口的定义如下: @SPI("javassist") ...
- [Vue] vue-cli3.0安装
1. node.js安装https://nodejs.org/en/download/ 2.npm的安装 由于新版的nodejs已经集成了npm,所以之前npm也一并安装好了.同样可以通过输入 &qu ...
- SQL Server 用户'NT AUTHORITY\IUSR' 登录失败
今天打开网站时,突然报这个错误,平时都好好的 Cannot open database "JMECC" requested by the login. The login fail ...
- vector源码1(参考STL源码--侯捷):源码
vector源码1(参考STL源码--侯捷) vector源码2(参考STL源码--侯捷) vector源码(参考STL源码--侯捷)-----空间分配导致迭代器失效 vector源码3(参考STL源 ...
- Android的断点续传的下载在线文件示例
Android的断点续传的下载在线文件示例 文件的结构如下: activity_main.xml: <LinearLayout xmlns:android="http://schema ...
- storm_常用命令
1)nimbus:启动nimbus守护进程 storm nimbus 2)supervisor:启动supervisor守护进程 storm supervisor 3)ui ...