在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. 类库中使用WPF 资源文件

    1.类库的 后缀.csproj文件,第一个<PropertyGroup>中加入下面代码 <ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-b ...

  2. [leetcode.com]算法题目 - Sort Colors

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  3. Meet in the middle

    搜索是\(OI\)中一个十分基础也十分重要的部分,近年来搜索题目越来越少,逐渐淡出人们的视野.但一些对搜索的优化,例如\(A\)*,迭代加深依旧会不时出现.本文讨论另一种搜索--折半搜索\((meet ...

  4. 【062有新题】OCP 12c 062出现大量之前没有的新考题-16

    choose one Which users are created and can be used for database and host management of your DBaaS da ...

  5. execution表达式--小坑

    之前使用的都是eclipse,今天开始复习spring了,开始接触IDEA,在复习过程中,出了点小事, 复习到AOP开发时,有半自动和全自动之说,之前我看那期视频里面没讲过全自动,我就自己再那敲Dem ...

  6. PICE(3):CassandraStreaming - gRPC-CQL Service

    在上一篇博文里我们介绍了通过gRPC实现JDBC数据库的streaming,这篇我们介绍关于cassandra的streaming实现方式.如果我们需要从一个未部署cassandra的节点或终端上读取 ...

  7. D02-R语言基础学习

    R语言基础学习——D02 20190423内容纲要: 1.前言 2.向量操作 (1)常规操作 (2)不定长向量计算 (3)序列 (4)向量的删除与保留 3.列表详解 (1)列表的索引 (2)列表得元素 ...

  8. 【6】JMicro微服务-服务日志监控

    如非授权,禁止用于商业用途,转载请注明出处作者:mynewworldyyl   1. 微服务相关 在前面的1到5节中,总共涉及服务提供者,服务消费者,服务监听服务,发布订阅服务,熔断器服务5种类型的猪 ...

  9. 多态、抽象类、接口_DAY09

    1:多态(掌握) (1)多态概念:一种事物的多种形态 (2)体现:父类的引用指向其子类的实例对象;接口的引用指向其实现类的实例对象 (3)特点: 成员方法:编译看左边,运行看右边 运行:子类重写的方法 ...

  10. wordpress给文章添加缩略图

    百度是个好东西,翻了半个小时的文章,终于把这个问题解决了. 一个问题的解决方法很多,但要找到一个自己理解的方法,缺比较难找,不管怎样,多动手,可能弄着弄着就会了. 教程开始: 1.先去后台管理安装Ea ...