Swt/Jface tableViewer入门教程二(让TableViewer按照列排序)
有一个功能是我们常使用的,就是在列的头上点击一下,整个表的记录按照这个列来排序,再点击一下按照这个列的反序来排序。那JFace是如何实现这个功能的呢?在JFace中是通过一个排序器来实现的,就是ViewerSorter下边写出详细的步骤
一、定义一个sorter继承自ViewerSorter
import
java.util.Date;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerSorter;
public class Sorter extends ViewerSorter {
private static final int ID = 1;
private static final int NAME = 2;
private static final int SEX = 3;
private static final int AGE = 4;
private static final int CREATE_DATE = 5;
public static final Sorter ID_ASC = new Sorter(ID);
public static final Sorter ID_DESC = new Sorter(-ID);
public static final Sorter NAME_ASC = new Sorter(NAME);
public static final Sorter NAME_DESC = new Sorter(-NAME);
public static final Sorter SEX_ASC = new Sorter(SEX);
public static final Sorter SEX_DESC = new Sorter(-SEX);
public static final Sorter AGE_ASC = new Sorter(AGE);
public static final Sorter AGE_DESC = new Sorter(-AGE);
public static final Sorter CREATE_DATE_ASC = new Sorter(CREATE_DATE);
public static final Sorter CREATE_DATE_DESC = new Sorter(-CREATE_DATE);
private int sortType ;
private Sorter(int sortType){
this.sortType = sortType;
}
public int compare(Viewer viewer, Object e1, Object e2) {
People p1 = (People)e1;
People p2 = (People)e2;
switch(sortType){
case ID:{
Long l1 = p1.getId();
Long l2 = p2.getId();
return l1.compareTo(l2);
}
case -ID:{
Long l1 = p1.getId();
Long l2 = p2.getId();
return l2.compareTo(l1);
}
case NAME:{
String s1 = p1.getName();
String s2 = p2.getName();
return s1.compareTo(s2);
}
case -NAME:{
String s1 = p1.getName();
String s2 = p2.getName();
return s2.compareTo(s1);
}
case SEX:{
String s1 = p1.getSex();
String s2 = p2.getSex();
return s1.compareTo(s2);
}
case -SEX:{
String s1 = p1.getSex();
String s2 = p2.getSex();
return s2.compareTo(s1);
}
case AGE:{
Integer i1 = p1.getAge();
Integer i2 = p2.getAge();
return i1.compareTo(i2);
}
case -AGE:{
Integer i1 = p1.getAge();
Integer i2 = p2.getAge();
return i2.compareTo(i1);
}
case CREATE_DATE:{
Date d1 = p1.getCreateDate();
Date d2 = p2.getCreateDate();
d1.compareTo(d2);
}
case -CREATE_DATE:{
Date d1 = p1.getCreateDate();
Date d2 = p2.getCreateDate();
d2.compareTo(d1);
}
}
return 0;
}
}
二、在TableViewer上,为每一列加入事件监听器类似这样的结构
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerSorter;
public class Sorter extends ViewerSorter {
private static final int ID = 1;
private static final int NAME = 2;
private static final int SEX = 3;
private static final int AGE = 4;
private static final int CREATE_DATE = 5;
public static final Sorter ID_ASC = new Sorter(ID);
public static final Sorter ID_DESC = new Sorter(-ID);
public static final Sorter NAME_ASC = new Sorter(NAME);
public static final Sorter NAME_DESC = new Sorter(-NAME);
public static final Sorter SEX_ASC = new Sorter(SEX);
public static final Sorter SEX_DESC = new Sorter(-SEX);
public static final Sorter AGE_ASC = new Sorter(AGE);
public static final Sorter AGE_DESC = new Sorter(-AGE);
public static final Sorter CREATE_DATE_ASC = new Sorter(CREATE_DATE);
public static final Sorter CREATE_DATE_DESC = new Sorter(-CREATE_DATE);
private int sortType ;
private Sorter(int sortType){
this.sortType = sortType;
}
public int compare(Viewer viewer, Object e1, Object e2) {
People p1 = (People)e1;
People p2 = (People)e2;
switch(sortType){
case ID:{
Long l1 = p1.getId();
Long l2 = p2.getId();
return l1.compareTo(l2);
}
case -ID:{
Long l1 = p1.getId();
Long l2 = p2.getId();
return l2.compareTo(l1);
}
case NAME:{
String s1 = p1.getName();
String s2 = p2.getName();
return s1.compareTo(s2);
}
case -NAME:{
String s1 = p1.getName();
String s2 = p2.getName();
return s2.compareTo(s1);
}
case SEX:{
String s1 = p1.getSex();
String s2 = p2.getSex();
return s1.compareTo(s2);
}
case -SEX:{
String s1 = p1.getSex();
String s2 = p2.getSex();
return s2.compareTo(s1);
}
case AGE:{
Integer i1 = p1.getAge();
Integer i2 = p2.getAge();
return i1.compareTo(i2);
}
case -AGE:{
Integer i1 = p1.getAge();
Integer i2 = p2.getAge();
return i2.compareTo(i1);
}
case CREATE_DATE:{
Date d1 = p1.getCreateDate();
Date d2 = p2.getCreateDate();
d1.compareTo(d2);
}
case -CREATE_DATE:{
Date d1 = p1.getCreateDate();
Date d2 = p2.getCreateDate();
d2.compareTo(d1);
}
}
return 0;
}
}
newColumnTableColumn.addSelectionListener(
new
SelectionAdapter()
{
boolean asc = true;
public void widgetSelected(SelectionEvent e){
tableViewer.setSorter(asc?Sorter.ID_ASC:Sorter.ID_DESC);
asc = !asc;
}
} );
都加入后TestTableViewer的结果:
boolean asc = true;
public void widgetSelected(SelectionEvent e){
tableViewer.setSorter(asc?Sorter.ID_ASC:Sorter.ID_DESC);
asc = !asc;
}
} );
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;
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);
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;
}
});
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();
}
}
}
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;
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);
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;
}
});
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