如何在SWT中按列对表进行排序

时间:2012-07-27 05:48:53

标签: java mysql sorting swt

我正在创建一个程序,从MySQL中的数据库获取数据并将其放入SWT表中,我对表的排序方式是删除所有TableItem并从数据库再次加载所有已排序的数据但是在那里数据库中有很多行需要大量加载数据

我尝试了一些代码,我发现这些代码对表格进行排序但是它们不起作用,我不知道我是否做得对,但是没有工作

有没有简单的方法来对SWT表进行排序?

3 个答案:

答案 0 :(得分:4)

这里有一个适当的代码示例,用于对swt列进行排序:

import org.eclipse.swt.*;  
 import org.eclipse.swt.widgets.*;  
 import org.eclipse.swt.layout.*;  
 import java.text.*;  
 import java.util.*;  

public class SortTable {  

  public static void main(String[] args) {  
     Display display = new Display ();  
     Shell shell = new Shell (display);  
     shell.setLayout(new FillLayout());  
     final Table table = new Table(shell, SWT.BORDER);  
     table.setHeaderVisible(true);  
     final TableColumn column1 = new TableColumn(table, SWT.NONE);  
     column1.setText("Column 1");  
     final TableColumn column2 = new TableColumn(table, SWT.NONE);  
     column2.setText("Column 2");  
     TableItem item = new TableItem(table, SWT.NONE);  
     item.setText(new String[] {"a", "3"});  
     item = new TableItem(table, SWT.NONE);  
     item.setText(new String[] {"b", "2"});  
     item = new TableItem(table, SWT.NONE);  
     item.setText(new String[] {"c", "1"});  
     column1.setWidth(100);  
     column2.setWidth(100);  
     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 == column1 ? 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].dispose();  
                         TableItem item = new TableItem(table, SWT.NONE, j);  
                         item.setText(values);  
                         items = table.getItems();  
                         break;  
                     }  
                 }  
             }  
             table.setSortColumn(column);  
         }  
     };  
     column1.addListener(SWT.Selection, sortListener);  
     column2.addListener(SWT.Selection, sortListener);  
     table.setSortColumn(column1);  
     table.setSortDirection(SWT.UP);  
     shell.setSize(shell.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, 300);  
     shell.open();  
     while (!shell.isDisposed ()) {  
         if (!display.readAndDispatch ()) display.sleep ();  
     }  
     display.dispose ();  
 }  

 } 

答案 1 :(得分:0)

我认为您不需要删除tableItems中的现有数据。只需调用将覆盖现有数据的table.getItems()

答案 2 :(得分:0)

如果我们使用Viewer作为List的输入,我们可以使用Collections.sort()方法。 我们可以创建一个通用的分类器,它将一个字段作为参数和查看器(以获取数据模型)

void createColumn(String fieldName){
Field field = MyClass.getDeclaredField(fieldName);
coltName = new TableViewerColumn(viewer, swtModifier);
coltName.getColumn().addSelectionListener(createSorter(viewer, field));
}


SelectionListener createSorter(final TableViewer viewer, final Field field) {
    return new SelectionListener() {

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public void widgetSelected(SelectionEvent e) {
Object data= viewer.getInput();
 viewer.getTable().setSortColumn((TableColumn) e.getSource());
 int oldSortDir = viewer.getTable().getSortDirection();
int newSortDir = SWT.UP;
switch(oldSortDir){
    case SWT.UP:
        newSortDir = SWT.DOWN;
        break;
    case SWT.DOWN:
        newSortDir = SWT.UP;
        break;
}
viewer.getTable().setSortDirection(newSortDir);
if(data instanceof List){
    Collections.sort((List) data, createComparator(field));
    viewer.refresh();
}           
}

@Override
public void widgetDefaultSelected(SelectionEvent e) {
    widgetSelected(e);
}
};
}

@SuppressWarnings("rawtypes")
Comparator createComparator(final Field field) {
    return new Comparator() {
        @Override
        public int compare(Object arg0, Object arg1) {
            try {
                Object value0 = field.get(arg0);
                Object value1 = field.get(arg1);

                if(value0 instanceof Integer){
                    return ((Integer) value0).compareTo((Integer) value1);
                }

                if(value0 instanceof String){
                    return ((String) value0).compareToIgnoreCase((String) value1);
                }

                if(value0 instanceof Date){
                    return ((Date) value0).compareTo((Date) value1);
                }

                if(value0 instanceof BigDecimal){
                    return ((BigDecimal) value0).compareTo((BigDecimal) value1);
                }

                if(value0 instanceof Long){
                    return ((Long) value0).compareTo((Long) value1);
                }

                if(value0 instanceof Double){
                    return ((Double) value0).compareTo((Double) value1);
                }

                if(value0 instanceof Boolean){
                    return ((Boolean) value0).compareTo((Boolean) value1);
                }
            }
            catch (IllegalArgumentException e) {
                e.printStackTrace();
            }
            catch (IllegalAccessException e) {
                e.printStackTrace();
            }

            return 0;
        }};
}