有一个功能是我们常使用的,就是在列的头上点击一下,整个表的记录按照这个列来排序,再点击一下按照这个列的反序来排序。那JFace是如何实现这个功能的呢?
在JFace中是通过一个排序器来实现的,就是ViewerSorter下边写出详细的步骤
一、定义一个sorter继承自ViewerSorter
二、在TableViewer上,为每一列加入事件监听器类似这样的结构
都加入后TestTableViewer的结果:
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
public class TestTableViewer
{
private static Table table;
/**
* Launch the application
* @param args
*/
public static void main(String[] args) {
final Display display = Display.getDefault();
final Shell shell = new Shell();
shell.setSize(500, 375);
shell.setText("SWT Application");
//
final TableViewer tableViewer = new TableViewer(shell, SWT.CHECK|SWT.MULTI | SWT.FULL_SELECTION | SWT.BORDER|SWT.V_SCROLL|SWT.H_SCROLL);
table = tableViewer.getTable();
table.setLinesVisible(true);
table.setHeaderVisible(true);
table.setBounds(97, 79, 373, 154);
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
final TableColumn newColumnTableColumn = new TableColumn(table, SWT.NONE);
newColumnTableColumn.setWidth(39);
newColumnTableColumn.setText("ID");
//加入事件监听器
newColumnTableColumn.addSelectionListener(new SelectionAdapter(){
boolean asc = true;
public void widgetSelected(SelectionEvent e){
tableViewer.setSorter(asc?Sorter.ID_ASC:Sorter.ID_DESC);
asc = !asc;
}
});
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
final TableColumn newColumnTableColumn_1 = new TableColumn(table, SWT.NONE);
newColumnTableColumn_1.setWidth(85);
newColumnTableColumn_1.setText("姓名");
// 加入事件监听器
newColumnTableColumn_1.addSelectionListener(new SelectionAdapter(){
boolean asc = true;
public void widgetSelected(SelectionEvent e){
tableViewer.setSorter(asc?Sorter.NAME_ASC:Sorter.NAME_DESC);
asc = !asc;
}
});
final TableColumn newColumnTableColumn_2 = new TableColumn(table, SWT.NONE);
newColumnTableColumn_2.setWidth(41);
newColumnTableColumn_2.setText("性别");
// 加入事件监听器
newColumnTableColumn_2.addSelectionListener(new SelectionAdapter(){
boolean asc = true;
public void widgetSelected(SelectionEvent e){
tableViewer.setSorter(asc?Sorter.SEX_ASC:Sorter.SEX_DESC);
asc = !asc;
}
});
final TableColumn newColumnTableColumn_3 = new TableColumn(table, SWT.NONE);
newColumnTableColumn_3.setWidth(43);
newColumnTableColumn_3.setText("年龄");
// 加入事件监听器
newColumnTableColumn_3.addSelectionListener(new SelectionAdapter(){
boolean asc = true;
public void widgetSelected(SelectionEvent e){
tableViewer.setSorter(asc?Sorter.AGE_ASC:Sorter.AGE_DESC);
asc = !asc;
}
});
final TableColumn newColumnTableColumn_4 = new TableColumn(table, SWT.NONE);
newColumnTableColumn_4.setWidth(126);
newColumnTableColumn_4.setText("创建日期");
// 加入事件监听器
newColumnTableColumn_4.addSelectionListener(new SelectionAdapter(){
boolean asc = true;
public void widgetSelected(SelectionEvent e){
tableViewer.setSorter(asc?Sorter.CREATE_DATE_ASC:Sorter.CREATE_DATE_DESC);
asc = !asc;
}
});
tableViewer.setContentProvider(new ContentProvider());
tableViewer.setLabelProvider(new TableLabelProvider());
tableViewer.setInput(People.getPeople());
shell.open();
shell.setLayout(new FillLayout());
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
}
试一下结果是不是出来了?
好了,最后解释几点:
1,sorter中利用了jdk的compareTo来实现比较,当然你也可以根据自己的需求来实现。
2, sorter中利用了"-"符号来得到正负数字,用来表现升序、降序。
source下载:http://www.blogjava.net/Files/dreamstone/jface-2.rar
效果:
![](//images0.cnblogs.com/blog/499166/201309/12094849-3245aa51293d43bc87e465d55fa92ab2.png)
![](//images0.cnblogs.com/blog/499166/201309/12094859-41f48d4481904d75a9bf2ea70b040b54.png)