有一个功能是我们常使用的,就是在列的头上点击一下,整个表的记录按照这个列来排序,再点击一下按照这个列的反序来排序。那JFace是如何实现这个功能的呢?
在JFace中是通过一个排序器来实现的,就是ViewerSorter下边写出详细的步骤
一、定义一个sorter继承自ViewerSorter

import java.util.Date;

import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerSorter;

public class Sorter extends ViewerSorter {
        private static final int ID = 1;
        private static final int NAME = 2;
        private static final int SEX = 3;
        private static final int AGE = 4;
        private static final int CREATE_DATE = 5;
        
        public static final Sorter ID_ASC = new Sorter(ID);
        public static final Sorter ID_DESC = new Sorter(-ID);
        public static final Sorter NAME_ASC = new Sorter(NAME);
        public static final Sorter NAME_DESC = new Sorter(-NAME);
        public static final Sorter SEX_ASC = new Sorter(SEX);
        public static final Sorter SEX_DESC = new Sorter(-SEX);
        public static final Sorter AGE_ASC = new Sorter(AGE);
        public static final Sorter AGE_DESC = new Sorter(-AGE);
        public static final Sorter CREATE_DATE_ASC = new Sorter(CREATE_DATE);
        public static final Sorter CREATE_DATE_DESC = new Sorter(-CREATE_DATE);
        
        private int sortType ;
        private Sorter(int sortType){
            this.sortType = sortType;
        }
        public int compare(Viewer viewer, Object e1, Object e2) {
            People p1 = (People)e1;
            People p2 = (People)e2;
            switch(sortType){
                case ID:{
                    Long l1 = p1.getId();
                    Long l2 = p2.getId();
                    return l1.compareTo(l2);
                }
                case -ID:{
                    Long l1 = p1.getId();
                    Long l2 = p2.getId();
                    return l2.compareTo(l1);
                }
                case NAME:{
                    String s1 = p1.getName();
                    String s2 = p2.getName();
                    return s1.compareTo(s2);
                }
                case -NAME:{
                    String s1 = p1.getName();
                    String s2 = p2.getName();
                    return s2.compareTo(s1);
                }
                case SEX:{
                    String s1 = p1.getSex();
                    String s2 = p2.getSex();
                    return s1.compareTo(s2);
                }
                case -SEX:{
                    String s1 = p1.getSex();
                    String s2 = p2.getSex();
                    return s2.compareTo(s1);
                }
                case AGE:{
                    Integer i1 = p1.getAge();
                    Integer i2 = p2.getAge();
                    return i1.compareTo(i2);
                }
                case -AGE:{
                    Integer i1 = p1.getAge();
                    Integer i2 = p2.getAge();
                    return i2.compareTo(i1);
                }
                case CREATE_DATE:{
                    Date d1 = p1.getCreateDate();
                    Date d2 = p2.getCreateDate();
                    d1.compareTo(d2);
                }
                case -CREATE_DATE:{
                    Date d1 = p1.getCreateDate();
                    Date d2 = p2.getCreateDate();
                    d2.compareTo(d1);
                }
            }
            return 0;
        }
    }

二、在TableViewer上,为每一列加入事件监听器类似这样的结构

    newColumnTableColumn.addSelectionListener(new SelectionAdapter(){
            boolean asc = true;
            public void widgetSelected(SelectionEvent e){
                tableViewer.setSorter(asc?Sorter.ID_ASC:Sorter.ID_DESC);
                asc = !asc;
            }
        });

都加入后TestTableViewer的结果:

import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;

public class TestTableViewer {
    private static Table table;
    /**
     * Launch the application
     * @param args
     */
    public static void main(String[] args) {
        final Display display = Display.getDefault();
        final Shell shell = new Shell();
        shell.setSize(500, 375);
        shell.setText("SWT Application");
        //
        final TableViewer tableViewer = new TableViewer(shell, SWT.CHECK|SWT.MULTI | SWT.FULL_SELECTION | SWT.BORDER|SWT.V_SCROLL|SWT.H_SCROLL);
        
        table = tableViewer.getTable();
        table.setLinesVisible(true);
        table.setHeaderVisible(true);
        table.setBounds(97, 79, 373, 154);

        final TableColumn newColumnTableColumn = new TableColumn(table, SWT.NONE);
        newColumnTableColumn.setWidth(39);
        newColumnTableColumn.setText("ID");
        //加入事件监听器
        newColumnTableColumn.addSelectionListener(new SelectionAdapter(){
            boolean asc = true;
            public void widgetSelected(SelectionEvent e){
                tableViewer.setSorter(asc?Sorter.ID_ASC:Sorter.ID_DESC);
                asc = !asc;
            }
        });

        final TableColumn newColumnTableColumn_1 = new TableColumn(table, SWT.NONE);
        newColumnTableColumn_1.setWidth(85);
        newColumnTableColumn_1.setText("姓名");
//        加入事件监听器
        newColumnTableColumn_1.addSelectionListener(new SelectionAdapter(){
            boolean asc = true;
            public void widgetSelected(SelectionEvent e){
                tableViewer.setSorter(asc?Sorter.NAME_ASC:Sorter.NAME_DESC);
                asc = !asc;
            }
        });
        
        final TableColumn newColumnTableColumn_2 = new TableColumn(table, SWT.NONE);
        newColumnTableColumn_2.setWidth(41);
        newColumnTableColumn_2.setText("性别");
//        加入事件监听器
        newColumnTableColumn_2.addSelectionListener(new SelectionAdapter(){
            boolean asc = true;
            public void widgetSelected(SelectionEvent e){
                tableViewer.setSorter(asc?Sorter.SEX_ASC:Sorter.SEX_DESC);
                asc = !asc;
            }
        });
        
        final TableColumn newColumnTableColumn_3 = new TableColumn(table, SWT.NONE);
        newColumnTableColumn_3.setWidth(43);
        newColumnTableColumn_3.setText("年龄");
//        加入事件监听器
        newColumnTableColumn_3.addSelectionListener(new SelectionAdapter(){
            boolean asc = true;
            public void widgetSelected(SelectionEvent e){
                tableViewer.setSorter(asc?Sorter.AGE_ASC:Sorter.AGE_DESC);
                asc = !asc;
            }
        });
        
        final TableColumn newColumnTableColumn_4 = new TableColumn(table, SWT.NONE);
        newColumnTableColumn_4.setWidth(126);
        newColumnTableColumn_4.setText("创建日期");
//        加入事件监听器
        newColumnTableColumn_4.addSelectionListener(new SelectionAdapter(){
            boolean asc = true;
            public void widgetSelected(SelectionEvent e){
                tableViewer.setSorter(asc?Sorter.CREATE_DATE_ASC:Sorter.CREATE_DATE_DESC);
                asc = !asc;
            }
        });
        
        tableViewer.setContentProvider(new ContentProvider());
        tableViewer.setLabelProvider(new TableLabelProvider());
        tableViewer.setInput(People.getPeople());
        
        shell.open();
        shell.setLayout(new FillLayout());
        shell.layout();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
    }
}

试一下结果是不是出来了?
好了,最后解释几点:
1,sorter中利用了jdk的compareTo来实现比较,当然你也可以根据自己的需求来实现。
2,  sorter中利用了"-"符号来得到正负数字,用来表现升序、降序。
source下载:http://www.blogjava.net/Files/dreamstone/jface-2.rar

效果:

SWT的TableVierer的使用二(数据排序)的更多相关文章

  1. SWT的TableVierer的使用三(数据筛选和着色)

    如果我们想根据某一列来过滤记录,如何实现呢?很简单,定义一个过滤器filter.这里只演示定义一个过滤器的情况.现实中你可以定义多个灵活的过滤器,通过替换过滤器来实现各种各样的过滤.一.过滤器代码: ...

  2. 大数据【四】MapReduce(单词计数;二次排序;计数器;join;分布式缓存)

       前言: 根据前面的几篇博客学习,现在可以进行MapReduce学习了.本篇博客首先阐述了MapReduce的概念及使用原理,其次直接从五个实验中实践学习(单词计数,二次排序,计数器,join,分 ...

  3. PHP二维数据排序,二维数据模糊查询

    一.因为项目中的一个报表需要合并三个表的数据,所以分表查询再合并数据,利用PHP数组函数进行排序,搜索.三表合并后的数组结构如下: Array ( [0] => Array ( [history ...

  4. php 二维数据排序 排行榜

    php 二维数据排序 排行榜 $rateCount = array(); foreach($groupUsers as $user){ $rateCount[] = $user['rate']; } ...

  5. SSIS 对数据排序

    SSIS 对数据排序有两种方式,一种是使用Sort组件,一种是使用sql command的order by clause进行排序. 一,使用Sort组件进行排序 SortType:升序 ascendi ...

  6. MapReduce二次排序

    默认情况下,Map 输出的结果会对 Key 进行默认的排序,但是有时候需要对 Key 排序的同时再对 Value 进行排序,这时候就要用到二次排序了.下面让我们来介绍一下什么是二次排序. 二次排序原理 ...

  7. Hadoop Mapreduce分区、分组、二次排序过程详解[转]

    原文地址:Hadoop Mapreduce分区.分组.二次排序过程详解[转]作者: 徐海蛟 教学用途 1.MapReduce中数据流动   (1)最简单的过程:  map - reduce   (2) ...

  8. Hadoop.2.x_高级应用_二次排序及MapReduce端join

    一.对于二次排序案例部分理解 1. 分析需求(首先对第一个字段排序,然后在对第二个字段排序) 杂乱的原始数据 排序完成的数据 a,1 a,1 b,1 a,2 a,2 [排序] a,100 b,6 == ...

  9. (转)MapReduce二次排序

    一.概述 MapReduce框架对处理结果的输出会根据key值进行默认的排序,这个默认排序可以满足一部分需求,但是也是十分有限的.在我们实际的需求当中,往往有要对reduce输出结果进行二次排序的需求 ...

随机推荐

  1. Android使用ksoap2-android调用WebService学习

    之前主要做客户端UI交互,很少处理数据和接触服务端,但现在的移动设备根本不可能离得开网络连接,数据的交换.最近学习的是在android端如何去调用远程WebService,都说WebService是一 ...

  2. CImageList用法介绍

    图像列表控制(CImageList)是相同大小图像的一个集合,每个集合中均以0为图像的索引序号基数,图像列表通常由大图标或位图构成,其中包含透明位图模式.可以利用WINDOWS32位应用程序接口函数A ...

  3. tbb 线程安全concurrent_queue的性能

    tbb实现了线程安全的queue,这样程序员既可以不用和那些lock,mutex,criticalsection打交道,又大大提高性能,太给力了..比较的结果见代码中的注释.结果可以看出代码足足少一半 ...

  4. 可能性dp+减少国家HDU4336

    Card Collector Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Subm ...

  5. Android Fragement学习笔记(三)----PreferenceFragment的使用

    相信大家对Perference都比較熟悉了,也就是我们常说的偏好设置,首选项设置,能够保存一些数据,比如我们在上一次使用的时候的一些内容,希望在下一次启动后依旧生效,而不须要再进行配置那么麻烦.一般这 ...

  6. 黑客白皮书:如何成为一名黑客(附FAQ)

    内容一览 为什么会有这份文档? 什么是黑客? 黑客应有的态度 黑客的基本技能 黑客文化中的地位 黑客和书呆子(Nerd)的联系 风格的意义 其它资源 FAQ(常问问题解答)   作为Jargon Fi ...

  7. Java Runtime.getRuntime().exec() 执行带空格命令

    可执行文件路径如果包含空格,则在java中不能被获取到. 此时Debug一下,会发现 project=null. project.waitFor 的返回值为1.但是去源路径单击bat文件是可以正常运行 ...

  8. 96 Stocks APIs: Bloomberg, NASDAQ and E*TRADE

      Our API directory now includes 96 stocks APIs. The newest is the Eurex VALUES API. The most popula ...

  9. JAVA编程心得-JAVA实现CRC-CCITT(XMODEM)算法

    CRC即循环冗余校验码(Cyclic Redundancy Check):是数据通信领域中最常用的一种差错校验码,其特征是信息字段和校验字段的长度可以任意选定. 1 byte checksum CRC ...

  10. 你真的了解JAVA中与Webservice相关的规范和实现吗?

    非常多人在项目中使用Webservice,仅仅是知道怎样公布Webservice,怎样调用Webservice,但真要论其来龙去脉,还真不一定清楚. 一切一切还要从我们伟大的sun公司规范说起. JA ...