




已阅读5页,还剩9页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
/* *(#)SortManager.java 05/1/13 * *Copyright 2005 Swing4CN Group. All rights reserved.*/package org.swing2cn.table;import java.awt.*;import java.awt.event.*;import java.util.*;import javax.swing.*;import javax.swing.event.*;import javax.swing.table.*;import org.swing2cn.util.*;/* * * Title: Swing2CN Project * * Description: A Project for Swing Components * * Copyright: Copyright (c) 2005 * * Organization: Swing4CN Group * This Class is used to add sorting function the the any JTable instances or JTables Childrens. * This is a very convenient tool to plug in.Because it dont effect the table and the JTable * instance neednt to change the TableModel to achieve the sorting function. * Warning: * Due to this Class will use the TableHeader,so if your JTable instance do some special to TableHeader * may occur Exception unexcepted. * author G.T.M. * version 0.1 * Chinese Descriptions: * 这是一个很方便的类,使用者不需要为了实现排序功能而特别制定一个TableMoel。只需要像使用插入式工具那样 * 插入这个类,就可以方便地实现排序功能。 * 警告: * 由于该类的实现过程设计TableHeader的处理,如果用户的JTable实例设计TableHeader的处理,可能会出现意外。 */public class SortManager implements TableModelListener /* * upIcon * * UpDownArrow * * see #UpDownArrow */ final static Icon upIcon = new UpDownArrow(0); /* * downIcon * * UpDownArrow * * see #UpDownArrow */ final static Icon downIcon = new UpDownArrow(1); private JTable table; private TableModel dataModel; /* * sortColumn * * The current sorting column. * * Chinese Descriptions: * * 当前排序的列. * * */ private int sortColumn; private Row rows; /* * ascending * * The order of sorting. * * Chinese Descriptions: * * 排序的顺序。 */ private boolean ascending; /* * sortableColumns * Its used to point out which columns are sortable. * Chinese Descriptions: * 用来指出哪几列是需要排序功能的. * * */ private int sortableColumns; /* * SortManager * * The base way to use this class.It will sort all the columns when user * click it. * * Chinese Descriptions: * * 该类基本用法,默认能给所有的列排序。 * * * * param jtable JTable */ public SortManager(JTable jtable) rows = null; ascending = true; sortableColumns = null; table = jtable; int i=0; int length=jtable.getModel().getColumnCount(); final int columns=new intlength; for(;ilength;i+) columnsi=i; sortableColumns=columns; initialize(); /* * SortManager * The base way to use this class.It will sort the specified column * when user click it. * Chinese Descriptions: * 该类基本用法,默认能给指定的列排序。 * * * * param jtable JTable * param i int * The column user specify to sort. */ public SortManager(JTable jtable, int i) rows = null; ascending = true; sortableColumns = null; table = jtable; sortColumn = i; initialize(); /* * SortManager * The base way to use this class.It will sort the specified columns * when user click it. * Chinese Descriptions: * 该类基本用法,默认能给指定的一组列排序。 * * * * param jtable JTable * param ai int The columns user specify to sort. */ public SortManager(JTable jtable, int ai) this(jtable, ai0); sortableColumns = (int) ai.clone(); /* * initialize * Initializing the JTable came in,escpecailly to add the listeners to * JTable. * * Chinese Descriptions: * * 初始化JTable实例,并且添加相应的侦听。 */ public void initialize() dataModel = table.getModel(); (AbstractTableModel) dataModel).addTableModelListener(this); addMouseListener(table); JTableHeader jtableheader = table.getTableHeader(); jtableheader.setDefaultRenderer(createHeaderRenderer(); if (table.getRowCount() 0) reinitialize(); /* * createHeaderRenderer * * Create the specify HeaderRender for the table,can response the mouse * action to sort the column. * * Chinese Description: * * 为table创建能够相应鼠标时间排序的TableHeaderRender。 * * * * return TableCellRenderer */ protected TableCellRenderer createHeaderRenderer() DefaultTableCellRenderer defaultHeaderRenderer = new SortHeaderRenderer(); defaultHeaderRenderer.setHorizontalAlignment(0); defaultHeaderRenderer.setHorizontalTextPosition(2); return defaultHeaderRenderer; public void reinitialize() rows = null; if (table.getRowCount() 0) rows = new Rowtable.getRowCount(); for (int i = 0; i rows.length; i+) rowsi = new Row(); rowsi.index = i; if (columnIsSortable(sortColumn) sort(); /* * columnIsSortable * Check out that if the specify column is sortable. It depend on the * constructor. * * Chinese Descriptions: * * 判断指定的列是否可排序,该方法的运行逻辑由整个类的构造函数构造时决定。 * * * * param i int * return boolean * see #public SortModel(Container container1, JTable jtable, int i) * see #public SortModel(Container container1, JTable jtable, int ai) * see #sortColumn * see #sortablrColumns */ private boolean columnIsSortable(int i) if (rows != null) if (sortableColumns != null) for (int j = 0; j sortableColumns.length; j+) if (i = sortableColumnsj) return true; else return true; return false; /* * mouseClicked * The real Method to add mouselistener to JTable.Due to the * SortHeaderRender is a inner class of this class.So the sortcolumn user * want can share and be managed in whole class field. * Chinese Description: * * 这个才是真正的处理排序的方法之一,它对JTable添加鼠标时间侦听。获得用户想排序的某行,由于SortHeaderRender是内部类,所以可以共享其数据并处理。 * * param table JTable */ public void addMouseListener(final JTable table) table.getTableHeader().addMouseListener(new MouseAdapter() public void mouseClicked(MouseEvent mouseevent) int i = table.columnAtPoint(mouseevent.getPoint(); int j = table.convertColumnIndexToModel(i); /转换出用户想排序的列和底层数据的列,然后判断 if (!columnIsSortable(j) return; if (j = sortColumn) ascending = !ascending; else ascending = true; sortColumn = j; sort(); ); /* * sort * The main method to sort the rows depends on the specify column to * sort. * Chinese Descriptions: * 主要调用排序的方法。 * Fix bug:table.UpdateUI();- * table.recalidate(); * table.repaint(); * * see #resetData() */ public void sort() if (rows = null) return; else (AbstractTableModel) dataModel).removeTableModelListener(this); Arrays.sort(rows); resetData(); (AbstractTableModel) dataModel).fireTableDataChanged(); (AbstractTableModel) dataModel).addTableModelListener(this); table.revalidate(); table.repaint(); return; /* * resetData * * The last step to sort.We reset the sorted to the table and show the * changes. * * Chinese Descriptions: * * 排序的最后一步,把排好序的数据重新返回table里面。 * * see #sort() */ public void resetData() Vector data=new Vector(dataModel.getRowCount(); int i=0; for(;idataModel.getRowCount();i+) int j=0; final Vector vv=new Vector(dataModel.getColumnCount(); for(;jdataModel.getColumnCount();j+) vv.add(dataModel.getValueAt(i,j); data.add(vv); i=0; for(;irows.length;i+) if(rowsi.index!=i) int j=0; final Vector vv=(Vector)data.get(rowsi.index); for(;jdataModel.getColumnCount();j+) dataModel.setValueAt(vv.get(j),i,j); public void tableChanged(TableModelEvent tablemodelevent) reinitialize(); private class SortHeaderRenderer extends DefaultTableCellRenderer public Component getTableCellRendererComponent(JTable jtable, Object obj, boolean flag, boolean flag1, int i, int j) if (jtable != null) JTableHeader jtableheader = jtable.getTableHeader(); if (jtableheader != null) setForeground(jtableheader.getForeground(); setBackground(jtableheader.getBackground(); setFont(jtableheader.getFont(); setText(obj != null ? obj.toString() : ); int k = jtable.convertColumnIndexToModel(j); if (k = sortColumn) setIcon(ascending ? SortManager.upIcon : SortManager.downIcon); else setIcon(null); setBorder(UIManager.getBorder(TableHeader.cellBorder); return this; private class Row implements Comparable private Row() /* public int compareTo(Object obj) int result=compare(obj); Row row = (Row) obj; Vector row1=new Vector(dataModel.getColumnCount(); Vector row2=new Vector(dataModel.getColumnCount(); int i=0; for(;i=0) else int j=0; for(;jdataModel.getColumnCount();j+) dataModel.setValueAt(row1.get(j),row.index,j); dataModel.setValueAt(row2.get(j),index,j); return result; */ public int compareTo(Object obj) Row row = (Row) obj; /bug found and fixed by chenxing 05.1.24.Support Local String sorting,such as /Chinese. /Chinese Descriptions: /支持本地字符的比较排序,例如:中文排序。 java.text.Collator cnCollator = java.text.Collator.getInstance(Locale.getDefault(); / Object obj1 = dataModel.getValueAt(index, sortColumn); Object obj2 = dataModel.getValueAt(row.index, sortColumn); if (ascending) if (!(obj1 instanceof Comparable) return -1; if (!(obj2 instanceof Comparable) return 1; else / if(obj1 instanceof String|obj2 instanceof String) / return cnCpare(obj1,obj2); / / else / / return (Comparable) obj1).compareTo(obj2); / if (!(obj1 instanceof Comparable) return 1; if (!(obj2 instanceof Comparable) return -1; else / if(obj1 instanceof String|obj2 instanceof String) / return cnCpare(obj2,obj1); / / else / / return (Comparable) obj2).compareTo(obj1); / public int index; package org.swing2cn.util;import java.awt.*;import javax.swing.*;public class UpDownArrow implements Icon /* * size * * The size of the Direcation Icon.Due to the Icon is square,the height and * width of Icon is this value. * * Chinese Description: * * 图标的大小,由于图标为正方形,所以高和宽都返回该值。 * * */ private int size = 12; /* * UP * * Static value of direction for this class. * * */ public static final int U
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年低空经济下航空应急救援体系构建报告
- 2025年低空经济无人机产业链完善与社会接受度培育报告
- 土建工程施工专项方案
- 2025年低空经济空域改革试点对航空产业智能化发展报告
- 2025年网络安全知识竞赛培训试题及答案
- 2025考研机械制造技术基础先进制造技术试题卷及答案
- 2025年低空经济「航空器设计制造」技术创新与产业发展报告
- 2025年全球低空经济「仓储物流」无人机技术应用前景分析报告
- 2025年低空经济飞行器生态修复技术研发与产业合作报告
- 辽宁省大连市高中化学 第一章 化学反应与能量 1.3 焓变 反应热习题课说课稿 新人教版选修4
- DB4201∕T 630.1-2020 中小学生研学旅行 第1部分:服务机构评定与服务规范
- 学生文明上网班会课件
- 叮当快药大健康生态圈战略解析
- 数学评比活动方案
- TCPUMT 034-2025 工业数字孪生 数字模型与数据集成交换要求
- 曹植的故事课件小学生
- 【艾瑞咨询】2024年中国健康管理行业研究报告494mb
- 施工作业安全管理制度
- 2025年房地产经纪人考试题及答案
- 4.3禁止生物武器
- 康复治疗技术专业实训室设计方案
评论
0/150
提交评论