📄 mytablesorter.java
字号:
package testSwing.mytable;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.*;
/**
* <p>Title: MyTableSorter</p>
* <p>Description: 实现自己的TableSoter,功能如下:</p>
* 继承原有MyTableModel的功能
* 实现了切换排序功能Sorter(排序的图片默认)
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: pubinfo</p>
* @author chineseren
* @version 1.0
*/
public class MyTableSorter extends MyTableModel {
public MyTableSorter() {
reallocateIndexes();
}
public MyTableSorter(int rowCount, int columnCount) {
super(rowCount, columnCount);
reallocateIndexes();
}
public MyTableSorter(Vector columnNames, int rowCount) {
super(columnNames, rowCount);
reallocateIndexes();
}
public MyTableSorter(Object[] columnNames, int rowCount) {
super(columnNames, rowCount);
reallocateIndexes();
}
public MyTableSorter(Vector data, Vector columnNames) {
super(data, columnNames);
reallocateIndexes();
}
public MyTableSorter(Object[][] data, Object[] columnNames) {
super(data, columnNames);
reallocateIndexes();
}
/**
* A sorter for TableModels. The sorter has a model (conforming to TableModel)
* and itself implements TableModel. TableSorter does not store or copy
* the data in the TableModel, instead it maintains an array of
* integers which it keeps the same size as the number of rows in its
* model. When the model changes it notifies the sorter that something
* has changed eg. "rowsAdded" so that its internal array of integers
* can be reallocated. As requests are made of the sorter (like
* getValueAt(row, col) it redirects them to its model via the mapping
* array. That way the TableSorter appears to hold another copy of the table
* with the rows in a different order. The sorting algorthm used is stable
* which means that it does not move around rows when its comparison
* function returns 0 to denote that they are equivalent.
*
* @version 1.5 12/17/97
* @author Philip Milne, modified by me
*/
private final boolean debug = false;
private static final int UP = 0;
private static final int DOWN = 1;
private ImageIcon upIcon = new ImageIcon("images/uparrow.gif");//10*10.gif
private ImageIcon downIcon = new ImageIcon("images/downarrow.gif");
public ImageIcon getUpIcon(){
return this.upIcon;
}
public void setUpIcon(ImageIcon upIcon){
this.upIcon = upIcon;
}
public ImageIcon getDownIcon(){
return this.downIcon;
}
public void setDownIcon(ImageIcon downIcon){
this.downIcon = downIcon;
}
/*private String upIconPath = "images/uparrow.gif";//比如:"test/images/downarrow.gif"
private String downIconPath = "images/downarrow.gif";
public String getUpIconPath(){
return this.upIconPath;
}
public void setUpIconPath(String upIconPath){
this.upIconPath = upIconPath;
}
public String getDownIconPath(){
return this.downIconPath;
}
public void setDownIconPath(String downIconPath){
this.downIconPath = downIconPath;
}*/
private int[] indexes;
private Vector sortingColumns = new Vector();
private boolean ascending = true;
private int compares;
//For the table column header sort icon
int sortOrder = DOWN; //expected sort order
int sortColumn = -1; //sort which model column
int sortViewColumn = -1; //sort which view column
public int[] getIndices() {
return indexes;
}
// The mapping only affects the contents of the data rows.
// Pass all requests to these rows through the mapping array: "indexes".
public Object getValueAt(int aRow, int aColumn) {
checkModel();
return super.getValueAt(aRow, aColumn);
}
public void setValueAt(Object aValue, int aRow, int aColumn) {
checkModel();
super.setValueAt(aValue, aRow, aColumn);
}
public void tableChanged(TableModelEvent e) {
//System.out.println("Sorter: tableChanged");
reallocateIndexes();
super.tableChanged(e);
}
//生成列的带图片的HeaderRenderer
private DefaultTableCellRenderer createHeaderRenderer(String colName,Icon icon){
DefaultTableCellRenderer picLabel = new DefaultTableCellRenderer();
picLabel.setText(colName);
picLabel.setBorder(UIManager.getBorder("TableHeader.CellBorder"));
picLabel.setHorizontalAlignment(JLabel.CENTER);
picLabel.setBackground(java.awt.Color.lightGray);
picLabel.setForeground(java.awt.Color.blue);
if(null != icon){
picLabel.setIcon(icon);
}
return picLabel;
}
// Add a mouse listener to the Table to trigger a table sort
// when a column heading is clicked in the JTable.
public void addMouseListenerToHeaderInTable(JTable table) {
final MyTableSorter sorter = this;
final JTable tableView = table;
//turn off this, otherwise, it would highlight the column.
//tableView.setColumnSelectionAllowed(false);
MouseAdapter listMouseListener = new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
TableColumnModel columnModel = tableView.getColumnModel();
int viewColumn = columnModel.getColumnIndexAtX(e.getX());
debug("=======================================");
debug("view column=" + viewColumn);
int column = tableView.convertColumnIndexToModel(viewColumn);
debug("model column=" + column);
debug("old sorting column=" + sortColumn);
debug("old sorting view column=" + sortViewColumn);
debug("old sorting order=" + sortOrder);
if(e.getClickCount() == 1 && viewColumn != -1) {
// jump between columns, ascending and descending
sorter.sortByColumn(column,(sortColumn != column) || (sortOrder == DOWN));
sortColumn = column;
// now memorize this sorting column
// clear all column headers. Since we don't know which one
// has the sorting flag, so clear them all.
// don't try to memorize that flag(view column) because
// users can switch view columns without notifying you!
//目前列头即能采用JLabel显示成图片格式,也能用<img/>显示,而且还要用"file:///"相对于当前盘符的路径
DefaultTableCellRenderer picLabel;
/*int len = columnModel.getColumnCount();
for(int i = 0; i < len; i++) {
picLabel = sorter.createHeaderRenderer(tableView.getColumnName(i),null);
//columnModel.getColumn(i).setHeaderValue(picLabel.getText());
columnModel.getColumn(i).setHeaderRenderer(picLabel);
}*/
// now set the correct icon for the column
String columntitle = tableView.getColumnName(viewColumn);
columntitle = "<html><font color=blue><B>" + columntitle + "</B></font></html>";
if(sortViewColumn != viewColumn) {
//columntitle = "<html><img src='file:///"+sorter.downIconPath+"' border='0' width='10' height='10'><font color=blue>"
//+ columntitle + "</font></html>";
picLabel = sorter.createHeaderRenderer(columntitle,downIcon);
sortOrder = UP;
} else if(sortOrder == DOWN) {
//columntitle = "<html><img src='file:///"+sorter.downIconPath+"' border='0' width='10' height='10'><font color=blue>"
//+ columntitle + "</font></html>";
picLabel = sorter.createHeaderRenderer(columntitle,downIcon);
sortOrder = UP;
} else {
//columntitle = "<html><img src='file:///"+sorter.upIconPath+"' border='0' width='10' height='10'><font color=blue>"
//+ columntitle + "</font></html>";
picLabel = sorter.createHeaderRenderer(columntitle,upIcon);
sortOrder = DOWN;
}
tableView.getColumnModel().getColumn(viewColumn).setHeaderRenderer(picLabel);
//tableView.getColumnModel().getColumn(viewColumn).setHeaderValue(columntitle);
sortViewColumn = viewColumn;
// these two lines are working, so we need to figure out the height
tableView.getTableHeader().setPreferredSize(new java.awt.Dimension(0,25));
tableView.getTableHeader().resizeAndRepaint();
// Setting the preferred size on the column header renderer will not
// work when you are using rowheaders and corner components in the
// containing scrollpane. If the corner component is taller than your
// table header, it will not display fully. So instead, set the preferred
// size of the viewport containing your table/column header like this:
// scrollpane.getColumnHeader().setPreferredSize(new java.awt.Dimension(0, 100));
// however, for now, we just set this:
// JLabel l=(JLabel)tableView.getColumnModel().getColumn(viewColumn).getHeaderRenderer();
// l.setPreferredSize(new java.awt.Dimension(0,100));
// however, it's working for this app, since
// System.out.println(tableView.getColumnModel().getColumn(viewColumn).getHeaderRenderer().getClass().getName());
// returns
// lib.swing.table.JComponentTableCellRenderer
//MyTable排序之后会将各个列宽度平均化,所以要重新调整
try {
((MyTable)tableView).setColumnWidth();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -