使用虚拟表和虚拟树的确可以很大程度地提供UI界面的性能,尤其是在大数据量的情况下,对于TableViewer使用虚拟表很简单,只要在创建TableViewer的时候指定SWT.VIRTUAL即可

tableViewer = new TableViewer(container, SWT.FULL_SELECTION
| SWT.BORDER | SWT.HIDE_SELECTION|SWT.VIRTUAL);

在以前的版本,TableViewer要使用virtual table的时候,需要实现ILazyContentProvider接口,但是目前好像是不用了,下面附上两个eclipse社区提供的例子

Snippet030VirtualLazyTableViewer.java,使用的是实现ILazyContentProvider接口
import org.eclipse.jface.viewers.ILazyContentProvider;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

/**
* A simple TableViewer to demonstrate usage of an ILazyContentProvider. You can compare this snippet to the Snippet029VirtualTableViewer
* to see the small but needed difference.
*
*
@author Tom Schindl <tom.schindl@bestsolution.at>
*
*/
public class Snippet030VirtualLazyTableViewer {
private class MyContentProvider implements IStructuredContentProvider, ILazyContentProvider {
private TableViewer viewer;
private MyModel[] elements;

public MyContentProvider(TableViewer viewer) {
this.viewer = viewer;
}

/* (non-Javadoc)
* @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
*/
public Object[] getElements(Object inputElement) {
return elements;
}

/* (non-Javadoc)
* @see org.eclipse.jface.viewers.IContentProvider#dispose()
*/
public void dispose() {

}

/* (non-Javadoc)
* @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
*/
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
this.elements = (MyModel[])newInput;
}

public void updateElement(int index) {
viewer.replace(elements[index], index);
}

}

public class MyModel {
public int counter;

public MyModel(int counter) {
this.counter = counter;
}

public String toString() {
return "Item " + this.counter;
}
}

public Snippet030VirtualLazyTableViewer(Shell shell) {
final TableViewer v = new TableViewer(shell,SWT.VIRTUAL);
v.setLabelProvider(
new LabelProvider());
v.setContentProvider(
new MyContentProvider(v));
v.setUseHashlookup(
true);
MyModel[] model
= createModel();
v.setInput(model);
v.setItemCount(model.length);
// This is the difference when using a ILazyContentProvider

v.getTable().setLinesVisible(
true);
}

private MyModel[] createModel() {
MyModel[] elements
= new MyModel[10000];

for( int i = 0; i < 10000; i++ ) {
elements[i]
= new MyModel(i);
}

return elements;
}

/**
*
@param args
*/
public static void main(String[] args) {
Display display
= new Display ();
Shell shell
= new Shell(display);
shell.setLayout(
new FillLayout());
new Snippet030VirtualLazyTableViewer(shell);
shell.open ();

while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}

display.dispose ();

}

}

Snippet029VirtualTableViewer.java 更加方便,直接指定SWT.VIRTUAL即可
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

/**
* A simple TableViewer to demonstrate the usage of a standard content provider
* with a virtual table
*
*
@author Tom Schindl <tom.schindl@bestsolution.at>
*
*/
public class Snippet029VirtualTableViewer {
private class MyContentProvider implements IStructuredContentProvider {
private MyModel[] elements;

/*
* (non-Javadoc)
*
* @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
*/
public Object[] getElements(Object inputElement) {
return elements;
}

/*
* (non-Javadoc)
*
* @see org.eclipse.jface.viewers.IContentProvider#dispose()
*/
public void dispose() {

}

/*
* (non-Javadoc)
*
* @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer,
* java.lang.Object, java.lang.Object)
*/
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
this.elements = (MyModel[]) newInput;
}
}

public class MyModel {
public int counter;

public MyModel(int counter) {
this.counter = counter;
}

public String toString() {
return "Item " + this.counter;
}
}

public Snippet029VirtualTableViewer(Shell shell) {
long time1=System.currentTimeMillis();
final TableViewer v = new TableViewer(shell,SWT.VIRTUAL);
v.setLabelProvider(
new LabelProvider());
v.setContentProvider(
new MyContentProvider());
v.setUseHashlookup(
true);
MyModel[] model
= createModel();
v.setInput(model);

v.getTable().setLinesVisible(
true);
System.out.println(System.currentTimeMillis()
-time1);
}

private MyModel[] createModel() {
MyModel[] elements
= new MyModel[10000];

for (int i = 0; i < 10000; i++) {
elements[i]
= new MyModel(i);
}

return elements;
}

/**
*
@param args
*/
public static void main(String[] args) {
Display display
= new Display();
Shell shell
= new Shell(display);
shell.setLayout(
new FillLayout());
new Snippet029VirtualTableViewer(shell);
shell.open();

while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}

display.dispose();

}

}