SWT的TableVierer的使用二(数据排序)

本文介绍了如何在SWT的TableViewer中实现数据排序功能。通过创建一个继承自ViewerSorter的排序器,并为TableViewer的每一列添加事件监听器,可以实现点击列头进行升序和降序排序。详细步骤包括定义sorter并利用compareTo方法进行比较,以及通过'-'符号判断排序方向。附带源码下载链接。
摘要由CSDN通过智能技术生成

有一个功能是我们常使用的,就是在列的头上点击一下,整个表的记录按照这个列来排序,再点击一下按照这个列的反序来排序。那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:{
     
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值