Waldo Waldo - 1 year ago 182
Java Question

Sort JFace Treeviewer multiple column

I'd like the user to be able to sort the

anytime he wants to just by clicking the column header. However I don't know the proper way to do it.

I discovered that we can use
to sort different elements. However, I don't know how to set a listener to be able to ascending or descending sort properly.

Is there a way to have an ARROW automaticly with JFace Library to let the user choose descending or ascending in the column sort ?


Answer Source

This isn't really automatic.

You need to call TreeColumn.addSelectionListener to add a selection listener to deal with the clicks on the tree column headers.

You will then need to get the Tree from the TreeViewer with

Tree tree = viewer.getTree();

and then call


to set the indicated main sort column and

tree.setSortDirection(SWT.UP) // or SWT.DOWN

to set the indicated sort direction.



to make the viewer redo the sorting.

You will need to track the column(s) to sort on in your comparator.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download