SWT的TableVierer的使用二(数据排序)
有一个功能是我们常使用的,就是在列的头上点击一下,整个表的记录按照这个列来排序,再点击一下按照这个列的反序来排序。那JFace是如何实现这个功能的呢?
在JFace中是通过一个排序器来实现的,就是ViewerSorter下边写出详细的步骤
一、定义一个sorter继承自ViewerSorter
 import java.util.Date;
import java.util.Date;
 import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.Viewer; import org.eclipse.jface.viewers.ViewerSorter;
import org.eclipse.jface.viewers.ViewerSorter;
 public class Sorter extends ViewerSorter {
public class Sorter extends ViewerSorter { private static final int ID = 1;
        private static final int ID = 1; private static final int NAME = 2;
        private static final int NAME = 2; private static final int SEX = 3;
        private static final int SEX = 3; private static final int AGE = 4;
        private static final int AGE = 4; private static final int CREATE_DATE = 5;
        private static final int CREATE_DATE = 5; 
         public static final Sorter ID_ASC = new Sorter(ID);
        public static final Sorter ID_ASC = new Sorter(ID); public static final Sorter ID_DESC = 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_ASC = new Sorter(NAME); public static final Sorter NAME_DESC = 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_ASC = new Sorter(SEX); public static final Sorter SEX_DESC = 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_ASC = new Sorter(AGE); public static final Sorter AGE_DESC = 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_ASC = new Sorter(CREATE_DATE); public static final Sorter CREATE_DATE_DESC = new Sorter(-CREATE_DATE);
        public static final Sorter CREATE_DATE_DESC = new Sorter(-CREATE_DATE); 
         private int sortType ;
        private int sortType ; private Sorter(int sortType){
        private Sorter(int sortType){ this.sortType = sortType;
            this.sortType = sortType; }
        } public int compare(Viewer viewer, Object e1, Object e2) {
        public int compare(Viewer viewer, Object e1, Object e2) { People p1 = (People)e1;
            People p1 = (People)e1; People p2 = (People)e2;
            People p2 = (People)e2; switch(sortType){
            switch(sortType){ case ID:{
                case ID:{ Long l1 = p1.getId();
                    Long l1 = p1.getId(); Long l2 = p2.getId();
                    Long l2 = p2.getId(); return l1.compareTo(l2);
                    return l1.compareTo(l2); }
                } case -ID:{
                case -ID:{ Long l1 = p1.getId();
                    Long l1 = p1.getId(); Long l2 = p2.getId();
                    Long l2 = p2.getId(); return l2.compareTo(l1);
                    return l2.compareTo(l1); }
                } case NAME:{
                case NAME:{ String s1 = p1.getName();
                    String s1 = p1.getName(); String s2 = p2.getName();
                    String s2 = p2.getName(); return s1.compareTo(s2);
                    return s1.compareTo(s2); }
                } case -NAME:{
                case -NAME:{ String s1 = p1.getName();
                    String s1 = p1.getName(); String s2 = p2.getName();
                    String s2 = p2.getName(); return s2.compareTo(s1);
                    return s2.compareTo(s1); }
                } case SEX:{
                case SEX:{ String s1 = p1.getSex();
                    String s1 = p1.getSex(); String s2 = p2.getSex();
                    String s2 = p2.getSex(); return s1.compareTo(s2);
                    return s1.compareTo(s2); }
                } case -SEX:{
                case -SEX:{ String s1 = p1.getSex();
                    String s1 = p1.getSex(); String s2 = p2.getSex();
                    String s2 = p2.getSex(); return s2.compareTo(s1);
                    return s2.compareTo(s1); }
                } case AGE:{
                case AGE:{ Integer i1 = p1.getAge();
                    Integer i1 = p1.getAge(); Integer i2 = p2.getAge();
                    Integer i2 = p2.getAge(); return i1.compareTo(i2);
                    return i1.compareTo(i2); }
                } case -AGE:{
                case -AGE:{ Integer i1 = p1.getAge();
                    Integer i1 = p1.getAge(); Integer i2 = p2.getAge();
                    Integer i2 = p2.getAge(); return i2.compareTo(i1);
                    return i2.compareTo(i1); }
                } case CREATE_DATE:{
                case CREATE_DATE:{ Date d1 = p1.getCreateDate();
                    Date d1 = p1.getCreateDate(); Date d2 = p2.getCreateDate();
                    Date d2 = p2.getCreateDate(); d1.compareTo(d2);
                    d1.compareTo(d2); }
                } case -CREATE_DATE:{
                case -CREATE_DATE:{ Date d1 = p1.getCreateDate();
                    Date d1 = p1.getCreateDate(); Date d2 = p2.getCreateDate();
                    Date d2 = p2.getCreateDate(); d2.compareTo(d1);
                    d2.compareTo(d1); }
                } }
            } return 0;
            return 0; }
        } }
    }二、在TableViewer上,为每一列加入事件监听器类似这样的结构
 newColumnTableColumn.addSelectionListener(new SelectionAdapter(){
    newColumnTableColumn.addSelectionListener(new SelectionAdapter(){ boolean asc = true;
            boolean asc = true; public void widgetSelected(SelectionEvent e){
            public void widgetSelected(SelectionEvent e){ tableViewer.setSorter(asc?Sorter.ID_ASC:Sorter.ID_DESC);
                tableViewer.setSorter(asc?Sorter.ID_ASC:Sorter.ID_DESC); asc = !asc;
                asc = !asc; }
            } });
        });都加入后TestTableViewer的结果:
 import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewer; import org.eclipse.swt.SWT;
import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.Table; import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableColumn;
 public class TestTableViewer {
public class TestTableViewer { private static Table table;
    private static Table table; /**
    /** * Launch the application
     * Launch the application * @param args
     * @param args */
     */ public static void main(String[] args) {
    public static void main(String[] args) { final Display display = Display.getDefault();
        final Display display = Display.getDefault(); final Shell shell = new Shell();
        final Shell shell = new Shell(); shell.setSize(500, 375);
        shell.setSize(500, 375); shell.setText("SWT Application");
        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);
        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 = tableViewer.getTable(); table.setLinesVisible(true);
        table.setLinesVisible(true); table.setHeaderVisible(true);
        table.setHeaderVisible(true); table.setBounds(97, 79, 373, 154);
        table.setBounds(97, 79, 373, 154);
 final TableColumn newColumnTableColumn = new TableColumn(table, SWT.NONE);
        final TableColumn newColumnTableColumn = new TableColumn(table, SWT.NONE); newColumnTableColumn.setWidth(39);
        newColumnTableColumn.setWidth(39); newColumnTableColumn.setText("ID");
        newColumnTableColumn.setText("ID"); //加入事件监听器
        //加入事件监听器 newColumnTableColumn.addSelectionListener(new SelectionAdapter(){
        newColumnTableColumn.addSelectionListener(new SelectionAdapter(){ boolean asc = true;
            boolean asc = true; public void widgetSelected(SelectionEvent e){
            public void widgetSelected(SelectionEvent e){ tableViewer.setSorter(asc?Sorter.ID_ASC:Sorter.ID_DESC);
                tableViewer.setSorter(asc?Sorter.ID_ASC:Sorter.ID_DESC); asc = !asc;
                asc = !asc; }
            } });
        });
 final TableColumn newColumnTableColumn_1 = new TableColumn(table, SWT.NONE);
        final TableColumn newColumnTableColumn_1 = new TableColumn(table, SWT.NONE); newColumnTableColumn_1.setWidth(85);
        newColumnTableColumn_1.setWidth(85); newColumnTableColumn_1.setText("姓名");
        newColumnTableColumn_1.setText("姓名"); //        加入事件监听器
//        加入事件监听器 newColumnTableColumn_1.addSelectionListener(new SelectionAdapter(){
        newColumnTableColumn_1.addSelectionListener(new SelectionAdapter(){ boolean asc = true;
            boolean asc = true; public void widgetSelected(SelectionEvent e){
            public void widgetSelected(SelectionEvent e){ tableViewer.setSorter(asc?Sorter.NAME_ASC:Sorter.NAME_DESC);
                tableViewer.setSorter(asc?Sorter.NAME_ASC:Sorter.NAME_DESC); asc = !asc;
                asc = !asc; }
            } });
        }); 
         final TableColumn newColumnTableColumn_2 = new TableColumn(table, SWT.NONE);
        final TableColumn newColumnTableColumn_2 = new TableColumn(table, SWT.NONE); newColumnTableColumn_2.setWidth(41);
        newColumnTableColumn_2.setWidth(41); newColumnTableColumn_2.setText("性别");
        newColumnTableColumn_2.setText("性别"); //        加入事件监听器
//        加入事件监听器 newColumnTableColumn_2.addSelectionListener(new SelectionAdapter(){
        newColumnTableColumn_2.addSelectionListener(new SelectionAdapter(){ boolean asc = true;
            boolean asc = true; public void widgetSelected(SelectionEvent e){
            public void widgetSelected(SelectionEvent e){ tableViewer.setSorter(asc?Sorter.SEX_ASC:Sorter.SEX_DESC);
                tableViewer.setSorter(asc?Sorter.SEX_ASC:Sorter.SEX_DESC); asc = !asc;
                asc = !asc; }
            } });
        }); 
         final TableColumn newColumnTableColumn_3 = new TableColumn(table, SWT.NONE);
        final TableColumn newColumnTableColumn_3 = new TableColumn(table, SWT.NONE); newColumnTableColumn_3.setWidth(43);
        newColumnTableColumn_3.setWidth(43); newColumnTableColumn_3.setText("年龄");
        newColumnTableColumn_3.setText("年龄"); //        加入事件监听器
//        加入事件监听器 newColumnTableColumn_3.addSelectionListener(new SelectionAdapter(){
        newColumnTableColumn_3.addSelectionListener(new SelectionAdapter(){ boolean asc = true;
            boolean asc = true; public void widgetSelected(SelectionEvent e){
            public void widgetSelected(SelectionEvent e){ tableViewer.setSorter(asc?Sorter.AGE_ASC:Sorter.AGE_DESC);
                tableViewer.setSorter(asc?Sorter.AGE_ASC:Sorter.AGE_DESC); asc = !asc;
                asc = !asc; }
            } });
        }); 
         final TableColumn newColumnTableColumn_4 = new TableColumn(table, SWT.NONE);
        final TableColumn newColumnTableColumn_4 = new TableColumn(table, SWT.NONE); newColumnTableColumn_4.setWidth(126);
        newColumnTableColumn_4.setWidth(126); newColumnTableColumn_4.setText("创建日期");
        newColumnTableColumn_4.setText("创建日期"); //        加入事件监听器
//        加入事件监听器 newColumnTableColumn_4.addSelectionListener(new SelectionAdapter(){
        newColumnTableColumn_4.addSelectionListener(new SelectionAdapter(){ boolean asc = true;
            boolean asc = true; public void widgetSelected(SelectionEvent e){
            public void widgetSelected(SelectionEvent e){ tableViewer.setSorter(asc?Sorter.CREATE_DATE_ASC:Sorter.CREATE_DATE_DESC);
                tableViewer.setSorter(asc?Sorter.CREATE_DATE_ASC:Sorter.CREATE_DATE_DESC); asc = !asc;
                asc = !asc; }
            } });
        }); 
         tableViewer.setContentProvider(new ContentProvider());
        tableViewer.setContentProvider(new ContentProvider()); tableViewer.setLabelProvider(new TableLabelProvider());
        tableViewer.setLabelProvider(new TableLabelProvider()); tableViewer.setInput(People.getPeople());
        tableViewer.setInput(People.getPeople()); 
         shell.open();
        shell.open(); shell.setLayout(new FillLayout());
        shell.setLayout(new FillLayout()); shell.layout();
        shell.layout(); while (!shell.isDisposed()) {
        while (!shell.isDisposed()) { if (!display.readAndDispatch())
            if (!display.readAndDispatch()) display.sleep();
                display.sleep(); }
        } }
    } }
}试一下结果是不是出来了?
好了,最后解释几点:
1,sorter中利用了jdk的compareTo来实现比较,当然你也可以根据自己的需求来实现。
2,  sorter中利用了"-"符号来得到正负数字,用来表现升序、降序。
source下载:http://www.blogjava.net/Files/dreamstone/jface-2.rar
效果:


SWT的TableVierer的使用二(数据排序)的更多相关文章
- SWT的TableVierer的使用三(数据筛选和着色)
		如果我们想根据某一列来过滤记录,如何实现呢?很简单,定义一个过滤器filter.这里只演示定义一个过滤器的情况.现实中你可以定义多个灵活的过滤器,通过替换过滤器来实现各种各样的过滤.一.过滤器代码: ... 
- 大数据【四】MapReduce(单词计数;二次排序;计数器;join;分布式缓存)
		前言: 根据前面的几篇博客学习,现在可以进行MapReduce学习了.本篇博客首先阐述了MapReduce的概念及使用原理,其次直接从五个实验中实践学习(单词计数,二次排序,计数器,join,分 ... 
- PHP二维数据排序,二维数据模糊查询
		一.因为项目中的一个报表需要合并三个表的数据,所以分表查询再合并数据,利用PHP数组函数进行排序,搜索.三表合并后的数组结构如下: Array ( [0] => Array ( [history ... 
- php 二维数据排序 排行榜
		php 二维数据排序 排行榜 $rateCount = array(); foreach($groupUsers as $user){ $rateCount[] = $user['rate']; } ... 
- SSIS 对数据排序
		SSIS 对数据排序有两种方式,一种是使用Sort组件,一种是使用sql command的order by clause进行排序. 一,使用Sort组件进行排序 SortType:升序 ascendi ... 
- MapReduce二次排序
		默认情况下,Map 输出的结果会对 Key 进行默认的排序,但是有时候需要对 Key 排序的同时再对 Value 进行排序,这时候就要用到二次排序了.下面让我们来介绍一下什么是二次排序. 二次排序原理 ... 
- Hadoop Mapreduce分区、分组、二次排序过程详解[转]
		原文地址:Hadoop Mapreduce分区.分组.二次排序过程详解[转]作者: 徐海蛟 教学用途 1.MapReduce中数据流动 (1)最简单的过程: map - reduce (2) ... 
- Hadoop.2.x_高级应用_二次排序及MapReduce端join
		一.对于二次排序案例部分理解 1. 分析需求(首先对第一个字段排序,然后在对第二个字段排序) 杂乱的原始数据 排序完成的数据 a,1 a,1 b,1 a,2 a,2 [排序] a,100 b,6 == ... 
- (转)MapReduce二次排序
		一.概述 MapReduce框架对处理结果的输出会根据key值进行默认的排序,这个默认排序可以满足一部分需求,但是也是十分有限的.在我们实际的需求当中,往往有要对reduce输出结果进行二次排序的需求 ... 
随机推荐
- Swift - 给表格UITableView添加索引功能(快速定位)
			像iOS中的通讯录,通过点击联系人表格右侧的字母索引,我们可以快速定位到以该字母为首字母的联系人分组. 要实现索引,我们只需要两步操作: (1)实现索引数据源代理方法 (2)响应点击索引触发的代理 ... 
- hdu 5014 思维题/推理
			http://acm.hdu.edu.cn/showproblem.php?pid=5014 从小数開始模拟找方法规律,然后推广,尤其敢猜敢尝试,错了一种思路继续猜-----这是一种非常重要的方法啊 ... 
- Effective C++_笔记_条款12_复制对象时勿忘其每一个成分
			(整理自Effctive C++,转载请注明.整理者:华科小涛@http://www.cnblogs.com/hust-ghtao/) 编译器会在必要时候为我们的classes创建copying函数, ... 
- C# MVC 自学笔记—2 MVC Movie简介
			MVC Movie是微软官方的一个MVC入门项目,我们可以跟着这个项目来实践入门 这是官方地址 http://www.asp.net/mvc/tutorials/mvc-4/getting-start ... 
- 3D-HEVC/HTM测试序列下载地址(官方完整版)
			最新3DV通用测试条件Common TestConditions of 3DV Core Experiments(JCT3V-E1100)中给出了所有标准测试序列的下载地址,有需要的朋友可以看看! 标 ... 
- Swift - AnyObject与Any的区别
			1,AnyObject :代表任何class类型的对象实例. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 class Man{ } class Woman{ ... 
- POJ 2632 Crashing Robots(较为繁琐的模拟)
			题目链接:http://poj.org/problem?id=2632 题目大意:题意简单,N个机器人在一个A*B的网格上运动,告诉你机器人的起始位置和对它的具体操作,输出结果: 1.Robot i ... 
- 很具体GC学习笔记
			GC学习笔记 这是我公司同事的GC学习笔记,写得蛮具体的,由浅入深,循序渐进,让人一看就懂,特转到这里. 一.GC特性以及各种GC的选择 1.垃圾回收器的特性 2.对垃圾回收器的选择 2.1 连续 V ... 
- 枚举类型互相转换(使用GetEnumName和TypeInfo两个函数)
			usesClasses,TypInfo ; typeTCommandType = (ctEmptyCommand,ctAdd,ctModify); TCommandTypeConvert=classp ... 
- Dom对象和JQuery对象的详细介绍及其区别
			一直搞不清Dom对象和JQuery对象之间的区别,今天好好总结下 1.dom对象(摘抄自百度百科http://baike.baidu.com/link?url=4L8bZ7kW6kE-it4F-1LU ... 
