代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
表格自动排序
作者:
jxqlovejava
/ 发布于
2012/6/12
/
600
表格自动排序
<div>public class SortableTableModel implements TableModel, TableModelListener { EventListenerList listenerList = new EventListenerList(); TableModel delegatedModel; int[] sortedIndicies; int sortColumn; Comparator comparator; Comparator[] comparators;</div> <div>public SortableTableModel (TableModel tm) { delegatedModel = tm; delegatedModel.addTableModelListener (this); comparators = new Comparator [tm.getColumnCount()]; sortedIndicies = new int [0]; setSortColumn (0);</div> <div>} // listener stuff public void addTableModelListener (TableModelListener l) { listenerList.add (TableModelListener.class, l); } <div>public void removeTableModelListener (TableModelListener l) { listenerList.remove (TableModelListener.class, l); } <div>public void fireTableModelEvent (TableModelEvent e) { Object[] listeners = listenerList.getListenerList(); for (int i = listeners.length-2; i>=0; i-=2) {</div> if (listeners[i] == TableModelListener.class) { ((TableModelListener) listeners[i+1]).tableChanged(e); } } } <div>// contents stuff public Class getColumnClass(int columnIndex) if (delegatedModel.getRowCount() > 0) return delegatedModel.getValueAt(0, columnIndex).getClass(); else return Object.class; } <div>public int getColumnCount() { return delegatedModel.getColumnCount(); } public String getColumnName (int index) { return delegatedModel.getColumnName (index); } public int getRowCount() { return delegatedModel.getRowCount(); } private int getDelegatedRow (int row) { return sortedIndicies [row]; } public Object getValueAt (int rowIndex, int columnIndex) { return delegatedModel.getValueAt (getDelegatedRow(rowIndex), columnIndex); } public boolean isCellEditable (int rowIndex, int columnIndex) { return delegatedModel.isCellEditable (rowIndex, columnIndex); } public void setValueAt (Object aValue, int rowIndex, int columnIndex) { delegatedModel.setValueAt (aValue, rowIndex, columnIndex); } <div>// internal helpers public void setComparatorForColumn (Comparator c, int i) {</div> <div>// range check if (i > comparators.length) { Comparator[] newComparators = new Comparator[i+1]; System.arraycopy (comparators, 0, newComparators, 0, comparators.length); comparators = newComparators; } // add the comparator comparators[i] = c; } public void setSortColumn (int i) { sortColumn = i;</div> <div>// reset current comparator, possibly to null, which // will make us use "natural ordering" for those values comparator = null; if ((comparators != null) && (comparators.length > 0)) // is there one in the list of comparators? comparator = comparators[sortColumn]; // now do the sort resort(); } <div>public int getSortColumn () { return sortColumn; } <div>protected void resort() { // does sortedIndicies need to grow or shrink? if (sortedIndicies.length != delegatedModel.getRowCount()) { sortedIndicies = new int [delegatedModel.getRowCount()]; } // build up a list of SortingDelegates ArrayList sortMe = new ArrayList(); for (int i=0; i<delegatedModel.getRowCount(); i++) {</div> <div>SortingDelegate sd = new SortingDelegate (delegatedModel.getValueAt(i, getSortColumn()), i); sortMe.add (sd); } // now sort him with the SortingDelegateComparator SortingDelegateComparator sdc =</div> <div>new SortingDelegateComparator (comparator); Collections.sort (sortMe, sdc);</div> <div>// fill sortedIndicies array // index -> value represents mapping from original // row to sorted row for (int i=0; i<sortMe.size(); i++) { sortedIndicies[i] = ((SortingDelegate) sortMe.get(i)).row; } <div>// fire change event fireAllChanged(); } <div>class SortingDelegate extends Object { public Object value; public int row; public SortingDelegate (Object v, int r) { value = v; row = r; } } class SortingDelegateComparator extends Object implements Comparator { Comparator comp; public SortingDelegateComparator (Comparator c) { comp = c; } public int compare (Object o1, Object o2) {</div> <div>Object v1 = ((SortingDelegate)o1).value; Object v2 = ((SortingDelegate)o2).value; if (comp != null)</div> <div>return comp.compare (v1, v2); else if (v1 instanceof Comparable) return ((Comparable)v1).compareTo (v2); else throw new IllegalArgumentException ("Can't compare objects "+ "for sorting"); } } <div>public void tableChanged (TableModelEvent e) { switch (e.getType()) { case TableModelEvent.DELETE: { resort(); fireAllChanged(); break; } <div>case TableModelEvent.INSERT: { resort(); fireAllChanged(); break; } case TableModelEvent.UPDATE: { resort(); fireAllChanged(); break; } <div>} } <div>protected void fireAllChanged() { TableModelEvent e = new TableModelEvent (this); fireTableModelEvent (e); } } </div>
试试其它关键字
表格自动排序
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
jxqlovejava
贡献的其它代码
(
6
)
.
通过XPath读取XML
.
下班倒计时(支持桌面通知)
.
表格自动排序
.
表格按列选择
.
自适应调整表格列宽
.
Java互斥锁的一个简单Demo
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3