TableViewer with Eclipse 3.2 - Sorter, Filter and Sort indicator
28. July 2006
In Eclipse 3.1 there was always the problem that you couldn't give a sort indicator to a sortable TableViewer. This issue was resolved with Eclipse 3.2. Today I want to show a simple TableViewer with a sorting and indicators, filters and a column reordering. Unfortunately the implementation for the sorters on JFace-Viewers don't support an ascending or desending flag that takes also care for the indicator-representation. We have to implement this for our own.
Defining the view
The first steps remain constant. Implement your view, label- and contenproviders. When you're defining the TableColumn-objects add a Listener to each TableColumn
-
tc0.addListener(SWT.Selection, sortListener);
Implement the Listener and Sorter-Coupling
Similar to this article we have to implement a sorter that holds ascending/descanding state. In Addition we have to set the indicator.
-
Listener sortListener = new Listener() {
-
// determine new sort column and direction
-
if (sortColumn == currentColumn) {
-
dir = dir == SWT.UP ? SWT.DOWN : SWT.UP;
-
} else {
-
dir = SWT.UP;
-
}
-
// sort the data based on column and direction
-
String sortIdentifier = null;
-
if (currentColumn == tc0) {
-
sortIdentifier = Sorter.MAIL_SORT;
-
}
-
if (currentColumn == tc1) {
-
sortIdentifier = Sorter.IP_SORT;
-
}
-
if (currentColumn == tc2) {
-
sortIdentifier = Sorter.DEPT_SORT;
-
}
-
if (currentColumn == tc3) {
-
sortIdentifier = Sorter.COMP_SORT;
-
}
-
}
-
};
Our sorter is instanciated with a direction
and the result is inverted if the current sort-direction is SWT.DOWN.
-
int returnValue = 0;
-
if (this.column == MAIL_SORT) {
-
returnValue = ((ResultItem) e1).getMail().compareTo(((ResultItem) e2).getMail());
-
}
-
if (this.column == IP_SORT) {
-
returnValue = ((ResultItem) e1).getIp().compareTo(((ResultItem) e2).getIp());
-
}
-
if (this.column == DEPT_SORT) {
-
returnValue = ((ResultItem) e1).getDepartment().compareTo(((ResultItem) e2).getDepartment());
-
}
-
if (this.column == COMP_SORT) {
-
returnValue = ((ResultItem) e1).getCompany().compareTo(((ResultItem) e2).getCompany());
-
}
-
if (this.dir == SWT.DOWN) {
-
returnValue = returnValue * -1;
-
}
-
return returnValue;
-
}
Downloads
Download the TableViewer-Demo as Plug-In (Source included - 35kByte)
Download the TableViewer-Demo as RCP (Source inluded - 9,3Mbyte)


Good article, very clear and easy to follow. It helped me a lot.
Comment by Bruno Negrao — 10. November 2006 @ 23:00
very good~~~
Comment by Leo — 22. December 2006 @ 09:16
Its nice, but I don’t found the source code.
Comment by Dominique — 24. December 2006 @ 13:14
The source is in the TableViewer-Demo as Plug-In download
Comment by Han — 4. September 2007 @ 21:53
Well done, thats what it´s got to be, your articles are always a pleasure. Greeting from munich.
Comment by Martin — 26. October 2007 @ 17:21
Good work. I was breaking my head with sorting tableviewer in RCP. Thanks for your tips I got it working. But I dont see the source in your attachment. It would be great if you added thesource.
Comment by Sarya — 2. May 2008 @ 15:52
thnx! helped me a lot..
Comment by shai ben hur — 15. February 2009 @ 13:42
Very Nice. Thanx a lot.
Comment by Michael — 25. February 2009 @ 18:36
Very nice example. Thanks.
Comment by davidallen — 30. October 2009 @ 06:42
This example helped me a lot.
Thank you.
Comment by Laurent — 21. May 2010 @ 12:02