Solved

How to create two directional sortable table in JAVA SWT?

Posted on 2013-11-18
3
640 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 500 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Progress

734 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