有一个功能是我们常使用的,就是在列的头上点击一下,整个表的记录按照这个列来排序,再点击一下按照这个列的反序来排序。那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用canvas绘制两种波纹效果

     波形效果有几种不同的呈现形式,比如从中间向四周散开的波形,也就是熟知的水涟漪:还有上下波动的曲线,像五线谱等.英文中可以称作Wave或者Ripple,所以暂且叫它们WaveView.WaveLayo ...

  2. 将 Shiro 作为一个许可为基础的应用程序 五:password加密/解密Spring应用

    考虑系统password的安全,眼下大多数系统都不会把password以明文的形式存放到数据库中. 一把会採取下面几种方式对password进行处理 password的存储 "编码" ...

  3. 表likp新增第一次过账输入日期字段,vl02n/vl01n/vl03n/vl06o的增强

    在程序:MV50AFZ1的 FORM USEREXIT_SAVE_DOCUMENT_PREPARE. *begin of ADD CRQ000000012135 CAIZJIAN 2014/3/25( ...

  4. WM_DRAWITEM与DrawItem()的讨论(自绘)

    http://blog.csdn.net/FlowShell/archive/2009/10/10/4648800.aspx 我在学习中经常遇到要重写DrawItem()的情况,但又有一个WM_DRA ...

  5. 我们究竟什么时候可以使用Ehcache缓存(转)

    一.Ehcache是什么 EhCache是Hibernate的二级缓存技术之一,可以把查询出来的数据存储在内存或者磁盘,节省下次同样查询语句再次查询数据库,大幅减轻数据库压力. 二.Ehcache的使 ...

  6. 移动端网页JS框架-手机触摸事件框架,日历框架带滑动效果

    swiper.js,hammer.js,mobiscroll http://www.mobiscroll.com/       日历

  7. 我是如何同时拿到阿里和腾讯offer的

    前言 三月真是一个忙碌的季节,刚刚开学就需要准备各种面试和笔试(鄙视).幸运的是,在长达一个月的面试内推季之后,终于同时拿到了阿里和腾讯的offer,还是挺开心的.突而想起久未更新的博客,就冒昧学一学 ...

  8. iOS国际化时遇到的错误:read failed: the data couldn't be read because it isn't in the correct format.

    事实上这个错误非常easy,就是当我们在国际化的时候,写key,写着写着就忘了加 ";" 所以查看一下自己的Localization文件就能够了

  9. 分分钟教会你使用HTML写Web页面

    在学习怎样使用HTML编写网页之前,我们必须先搞清楚什么是HTML?当然了不是系统的给大家介绍HTML的前世今生,假设对其身世感兴趣的小伙伴能够去问度娘,她会给你想要的答案. 所谓HTML,就是我们常 ...

  10. must return an Iterable of arrays.(junit4)

    java.lang.Exception: TestIterator.init() must return an Iterable of arrays. at org.junit.runners.Par ...