在SWT程序中使用table展現數據時,若是數據過多,執行起來會比較慢,不過,咱們能夠藉助VirtualTable來解決這一問題。html
Eclipse官網中關於VirtualTable的說明見:http://www.eclipse.org/articles/Article-SWT-Virtual/Virtual-in-SWT.htmlshell
先來看一個不用VirtualTable的demo:eclipse
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,爲了顯示執行時間,代碼中加入了耗時打印~~oop
執行如下,輸出以下:性能
Create data cost:118
All cost:4783
先不急着評價,來看看VirtualTable的Demo:spa
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,執行如下,看看輸出結果:code
create data cost:118
All cost:181
一個是4783ms,一個是181ms,孰優孰劣不言自明!htm
使用virtual table很簡單,就三個要點:blog
①在建立表時,加上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
經此三個小步驟,你的表格性能將大幅獲得提高