| |
表格排序:点击表头进行表排序 |
|
|
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.GregorianCalendar;
import java.util.Vector;
import javax.swing.ImageIcon;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.TableColumnModelEvent;
import javax.swing.event.TableColumnModelListener;
import javax.swing.event.TableModelEvent;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;
public class TableSortDemo extends JFrame{
protected JTable table = new JTable();
protected MyTableModel tableModel;
protected JLabel titleLabel = new JLabel("Click table header to sort the column.");
public TableSortDemo() {
super();
setSize(600, 300);
tableModel = new MyTableModel();
getContentPane().add(titleLabel, BorderLayout.NORTH);
table.setModel(tableModel);
JTableHeader header = table.getTableHeader();
header.setUpdateTableInRealTime(true);
header.addMouseListener(tableModel.new ColumnListener(table));
header.setReorderingAllowed(true);
JScrollPane ps = new JScrollPane();
ps.getViewport().add(table);
getContentPane().add(ps, BorderLayout.CENTER);
WindowListener wndCloser = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
};
addWindowListener(wndCloser);
setVisible(true);
}
public static void main(String argv[]) {
new TableSortDemo();
}
}
class MyTableModel extends AbstractTableModel {
protected int sortCol = 0;
protected boolean isSortAsc = true;
protected int m_result = 0;
protected int columnsCount = 1;
Vector vector = new Vector();
public MyTableModel() {
vector.removeAllElements();
vector.addElement(new Integer(24976600));
vector.addElement(new Integer(24));
vector.addElement(new Integer(2497));
vector.addElement(new Integer(249766));
vector.addElement(new Integer(2497660));
vector.addElement(new Integer(6600));
vector.addElement(new Integer(76600));
vector.addElement(new Integer(976600));
vector.addElement(new Integer(4976600));
}
public int getRowCount() {
return vector == null ? 0 : vector.size();
}
public int getColumnCount() {
return columnsCount;
}
public String getColumnName(int column) {
String str = "data";
if (column == sortCol)
str += isSortAsc ? " >>" : " <<";
return str;
}
public boolean isCellEditable(int nRow, int nCol) {
return false;
}
public Object getValueAt(int nRow, int nCol) {
if (nRow < 0 || nRow >= getRowCount())
return "";
if(nCol>1){
return "";
}
return vector.elementAt(nRow);
}
public String getTitle() {
return "data ";
}
class ColumnListener extends MouseAdapter {
protected JTable table;
public ColumnListener(JTable t) {
table = t;
}
public void mouseClicked(MouseEvent e) {
TableColumnModel colModel = table.getColumnModel();
int columnModelIndex = colModel.getColumnIndexAtX(e.getX());
int modelIndex = colModel.getColumn(columnModelIndex)
.getModelIndex();
if (modelIndex < 0)
return;
if (sortCol == modelIndex)
isSortAsc = !isSortAsc;
else
sortCol = modelIndex;
for (int i = 0; i < columnsCount; i++) {
TableColumn column = colModel.getColumn(i);
column.setHeaderValue(getColumnName(column.getModelIndex()));
}
table.getTableHeader().repaint();
Collections.sort(vector,new MyComparator(isSortAsc));
table.tableChanged(new TableModelEvent(MyTableModel.this));
table.repaint();
}
}
}
class MyComparator implements Comparator {
protected boolean isSortAsc;
public MyComparator( boolean sortAsc) {
isSortAsc = sortAsc;
}
public int compare(Object o1, Object o2) {
if (!(o1 instanceof Integer) || !(o2 instanceof Integer))
return 0;
Integer s1 = (Integer) o1;
Integer s2 = (Integer) o2;
int result = 0;
result = s1.compareTo(s2);
if (!isSortAsc)
result = -result;
return result;
}
public boolean equals(Object obj) {
if (obj instanceof MyComparator) {
MyComparator compObj = (MyComparator) obj;
return compObj.isSortAsc == isSortAsc;
}
return false;
}
}
|
|
|
Related examples in the same category |
1. | 创建一个表格 | | | 2. | Creates tables that allow rows and columns to be added or deleted | | | 3. | 从列表数据和列名建立一个表 | | | 4. | Getting the Number of Rows and Columns in a JTable Component | | | 5. | 添加数据到表格组件 | | | 6. | 通过DefaultTableModel添加列到表格 | | | 7. | 通过DefaultTableModel插入一行到表格 | | | 8. | Insert a row to a table through DefaultTableModel at specified row | | | 9. | 滚动表 | | | 10. | 简单示范表格 | | | 11. | 创建一个表从两个二维数组 | | | 12. | 创建表与Unicode数据 | | | 13. | 使用表,以显示输入的数据(整数,布尔,彩色) | | | 14. | 调整表的大小 | | | 15. | 费用表 | | | 16. | StockTable 6:事件和动态显示 | | | 17. | 表格选择事件 | | | 18. | 表行和列的选择 | | | 19. | 表选择模式 | | | 20. | Use code to change Table Selection | | | 21. | 结果显示在表(表格) | | | 22. | 表格与JDBC结果 | | | 23. | 结果表:继承AbstractTable和JDBC结果集ResultSet | | | 24. | 列表界面性能,表格和排序 | | | 25. | 属性表:使用表格来显示和编辑属性 | | | 26. | 表与工具提示,单元格和列标题 | | | 27. | StockTable 4 :表排序 | | | 28. | Table Sort Test | | | 29. | 创建表格的图像 | | | 30. | JTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); | | | 31. | JTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); | | | 32. | JTable.setColumnSelectionAllowed(boolean b); | | | 33. | JTable.setRowSelectionAllowed(boolean b); | | | 34. | JTable.setCellSelectionEnabled(boolean b); | | | 35. | 处理选择和模型的变化事件 | | | 36. | ListSelectionModel rowSelMod = JTable.getSelectionModel(); | | | 37. | ListSelectionModel colSelMod = JTable.getColumnModel().getSelectionModel(); | | | 38. | JTree.getModel().addTreeModelListener(new TreeModelListener()) | | | 39. | Move the last visible column so it becomes the first visible column | | | 40. | the last column is moved to the first position | | | 41. | 在一个表格允许用户调整列 | | | 42. | 检索表中可见单元格的值 | | | 43. | 从模型中检索表中的数据 | | | 44. | Change a cell in the 2nd visible column | | | 45. | Change a cell in the 3rd column in the model | | | 46. | 在非可卷动的表格显示表头 | | | 47. | 在一个表格改变列名称 | | | 48. | 在表头显示图标 | | | 49. | Implementing Variable-Height Column Headers in a JTable Component | | | 50. | Removing the Column Headers from a Scrollable in a JTable Component | | | 51. | Creating a Custom Column Header Renderer in a JTable Component | | | 52. | Setting Column Header Tool Tips in a JTable Components | | | 53. | 对表格单元格组件设置工具提示 | | | 54. | 在表格使行默认选择 | | | 55. | 在表格设置有效列 | | | 56. | 选择表格单元格 | | | 57. | 得到默认的选择模式: MULTIPLE_INTERVAL_SELECTION | | | 58. | 只允许单一选择 | | | 59. | Allow selection to span one contiguous set of rows, visible columns, or block of cells | | | 60. | Allow multiple selections of rows, visible columns, or cell blocks (default) | | | 61. | 选择一个列 | | | 62. | 选择一个系列 | | | 63. | 取消一系列栏的选择 | | | 64. | 选中某行-列0 | | | 65. | 选择一个额外的一系列行 | | | 66. | 取消一系列行的选择 | | | 67. | 选择一个单元格:单元格( 2,1 ) | | | 68. | 将选定内容扩展到包括所有单元格( 5,3 ) | | | 69. | Deselect a cell: cell (3,2), All cells in the row and column containing (3,2) are deselected. | | | 70. | Toggles the selection state, if it were called again, it exactly reverses the first call. | | | 71. | 选择所有单元格 | | | 72. | 取消所有单元格 | | | 73. | Column selection is enabled, get the indices of the selected columns | | | 74. | Row selection is enabled, Get the indices of the selected rows | | | 75. | 活动被选定行的索引 | | | 76. | 获得最大和最小范围内选定的单元格 | | | 77. | Check each cell in the min and max ranges of selected cells | | | 78. | 在一个表格禁用选则 | | | 79. | 获得选中的表单元格 | | | 80. | 建立一个可滚动表格 | | | 81. | Disable auto resizing to make the table horizontal scrollable | | | 82. | 确定是否一个单元格可见 | | | 83. | 使表格中的单元格可见 | | | 84. | 滚动单元格 | | | 85. | 显示横向和纵向网格线(默认) | | | 86. | 不显示任何网格线 | | | 87. | 只显示垂直网格线 | | | 88. | 只显示水平网格线 | | | 89. | 设置网格颜色 | | | 90. | 获取表格组件单元格之间间隙的大小 | | | 91. | 在单元格的左右添加空格 | | | 92. | 增加行高 | | | 93. | Programmatically Starting Cell Editing in a JTable Component | | | 94. | Save the current value in the cell being edited and stops the editing process | | | 95. | Discard any changes made by the user and stops the editing process | | | 96. | 禁用用户编辑表格 | | | 97. | 禁用用户编辑表格与DefaultTableModel | | | 98. | 基于列在一个表格排序列 | | | 99. | 表格排序列 | | | 100. | 表格组件选择事件监听 | | | 101. | Listening for Changes to the Rows and Columns of a JTable Component | | | 102. | 在一个表格监听列相关的变化事件 | | | 103. | Listening for Clicks on a Column Header in a JTable Component | | | 104. | 排序和筛选表 | | | 105. | 使用regexFilter过滤表 | | | 106. | 设置表格行高度 | | |
|