2

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

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

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

4

3 回答 3

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 ();  
 }  

 } 
于 2012-07-27T05:59:41.930 回答
0

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

于 2012-07-27T05:55:16.730 回答
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;
        }};
}
于 2015-01-04T20:34:36.977 回答