troubleshooting Question

How to create two directional sortable table in JAVA SWT?

Avatar of Tolgar
Tolgar asked on
3 Comments1 Solution879 ViewsLast Modified:
I am working on creating a GUI using Java SWT package and there is a table in this GUI.

I want to be able to sort the items in this table based on certain columns. In order to do this, I use the following code but I have a problem with it.

This code only sorts the columns in one direction.

What I want to do is to sort them in both directions when I click on the column header.

Can you please help me in changing this code to do this?

	            //This is to sort the columns in the table
	            Listener sortListener = new Listener() {
	                public void handleEvent(Event e) {
	                    TableItem[] items = table.getItems();
	                    Collator collator = Collator.getInstance(Locale.getDefault());
	                    TableColumn column = (TableColumn) e.widget;
	                    int index = column == table.getColumn(0) ? 0 : 1;
	                    for (int i = 1; i < items.length; i++) {
	                        String value1 = items[i].getText(index);
	                        for (int j = 0; j < i; j++) {
	                            String value2 = items[j].getText(index);
	                            if (, value2) < 0) {
	                                String[] values = { items[i].getText(0),
	                                        items[i].getText(1), items[i].getText(2),
	                                        items[i].getText(3) };
	                                TableItem item = new TableItem(table, SWT.NONE, j);
	                                items = table.getItems();
	            table.getColumn(1).addListener(SWT.Selection, sortListener);
	            table.getColumn(2).addListener(SWT.Selection, sortListener);
IT Business Systems Analyst / Software Developer
Join our community to see this answer!
Unlock 1 Answer and 3 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros