Waldo Waldo - 4 months ago 39
Java Question

Sort JFace Treeviewer multiple column

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

TreeViewer
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
ViewerComparator
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 ?

Regards,
Waldo

Answer

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

tree.setSortColumn(treeColumn);

to set the indicated main sort column and

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

to set the indicated sort direction.

Call

viewer.refresh();

to make the viewer redo the sorting.

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

Comments