Solved

How to create two directional sortable table in JAVA SWT?

Posted on 2013-11-18
3
618 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 35

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 35

Expert Comment

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

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Html Table Looping (part 2) 5 35
Java basic valueOf question 1 34
Assignment from incompatible pointer type? 2 40
$_GET call between URL 3 45
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to implement Singleton Design Pattern in Java.

726 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