Solved

How to create two directional sortable table in JAVA SWT?

Posted on 2013-11-18
3
571 Views
Last Modified: 2013-11-18
Hi,
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 (collator.compare(value1, value2) < 0) {
	                                String[] values = { items[i].getText(0),
	                                        items[i].getText(1), items[i].getText(2),
	                                        items[i].getText(3) };
	                                items[i].dispose();
	                                TableItem item = new TableItem(table, SWT.NONE, j);
	                                item.setText(values);
	                                items = table.getItems();
	                                break;
	                            }
	                        }
	                    }
	                    table.setSortColumn(column);
	                }
	            };
	            
	            
	            table.getColumn(1).addListener(SWT.Selection, sortListener);
	            table.getColumn(2).addListener(SWT.Selection, sortListener);
	            table.setSortColumn(table.getColumn(1));
	            table.setSortDirection(SWT.TOP);
	            
	            table.setRedraw(true);
            }
          });

Open in new window

0
Comment
Question by:Tolgar
  • 2
3 Comments
 
LVL 35

Accepted Solution

by:
mccarl earned 500 total points
Comment Utility
Try something like this... (untested as I can't really run your code)
	            //This is to sort the columns in the table
	            Listener sortListener = new Listener() {
	                int prevSortIndex = -1;
	                int prevSortDirection = 1;

	                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;
	                    if (prevSortIndex == index) {
	                        prevSortDir *= -1;
	                    } else {
	                        prevSortDir = 1;
	                        prevSortIndex = index;
	                    }
	                    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 ((collator.compare(value1, value2) * prevSortDir) < 0) {
	                                String[] values = { items[i].getText(0),
	                                        items[i].getText(1), items[i].getText(2),
	                                        items[i].getText(3) };
	                                items[i].dispose();
	                                TableItem item = new TableItem(table, SWT.NONE, j);
	                                item.setText(values);
	                                items = table.getItems();
	                                break;
	                            }
	                        }
	                    }
	                    table.setSortColumn(column);
	                }
	            };
	            
	            
	            table.getColumn(1).addListener(SWT.Selection, sortListener);
	            table.getColumn(2).addListener(SWT.Selection, sortListener);
	            table.setSortColumn(table.getColumn(1));
	            table.setSortDirection(SWT.TOP);
	            
	            table.setRedraw(true);
            }
          });

Open in new window

In case it's not obvious, the changes that I've made are...
Added lines 3 & 4, which just declares variables that will track the previous sorting state of the table
Added lines 11 - 16, which manipulate this state based on which column was clicked on. If it was the same column as last time then reverse the dir (dir *= -1) otherwise if it is a different column, then set it to order normally and save the column that was clicked so that we can test it next time.
Changed line 21, to use the "dir" variable to either do normal or reversed sorting. Collator.compare returns either negative, 0, or positive int depending on the order of value1 and value2. If we multiply it by -1, that logic is reversed and so the sort is reversed
0
 

Author Comment

by:Tolgar
Comment Utility
Wow! Fantastic!!

Thanks for your prompt response,
0
 
LVL 35

Expert Comment

by:mccarl
Comment Utility
Not a problem! ;)
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This video teaches viewers about errors in exception handling.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now