Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to create two directional sortable table in JAVA SWT?

Posted on 2013-11-18
3
Medium Priority
?
672 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 36

Accepted Solution

by:
mccarl earned 2000 total points
ID: 39658325
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
ID: 39658334
Wow! Fantastic!!

Thanks for your prompt response,
0
 
LVL 36

Expert Comment

by:mccarl
ID: 39658426
Not a problem! ;)
0

Featured Post

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
This video teaches viewers about errors in exception handling.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

722 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