|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjavax.swing.table.AbstractTableModel
org.afcs.warts.gui.TableMap
org.afcs.warts.gui.TableSorter
The TableSorter class adds sorting functionality to a TableModel instance, and can be integrated with a JTable to update the sort order whenever the user clicks on a column in the table header.
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.
A TableSorter instance can be added as a mouse listener to a JTable
using the listenTo(JTable)
method. The TableSorter can then handle
all events on behalf of the client, re-sorting data every time the user
clicks on a column in the table header, and updating the icons in the header
to reflect the current sort order.
This implementation is based on version 1.5 of Philip Milne's classic TableSorter code (I can't find the original implementation, but google finds lots of other people using it), but has been streamlined substantially with numerous major API changes.
LICENSE: This code is released to the public domain and may be used for any purpose whatsoever without permission or acknowledgment.
Field Summary |
Fields inherited from class javax.swing.table.AbstractTableModel |
listenerList |
Constructor Summary | |
TableSorter(javax.swing.table.TableModel model)
Constructs a new instance with the underlying model providing the unsorted data. |
Method Summary | |
int |
getOriginalRowIndex(int rowIndex)
Given the index of a row in the sorted model, this method returns the index of the same row in the unsorted model that was passed to this instance at initialisation. |
int |
getSortColumn()
Returns the index of the last column sorted on, or -1 if the model received at initialisation has no columns. |
java.lang.Object |
getValueAt(int rowIndex,
int columnIndex)
This method returns the object at the specified row and column in the sorted table model. |
boolean |
isSortAscending()
Returns true if the current sort column was last sorted in ascending direction. |
void |
listenTo(javax.swing.JTable table)
This method adds the current instance as a mouse listener to the header of the specified table. |
void |
setValueAt(java.lang.Object value,
int rowIndex,
int columnIndex)
This method sets the object at the specified row and column in the sorted table model. |
void |
sort(int columnIndex,
boolean sortAscending)
This method can be used to trigger a sort on the specified column in the specified direction. |
void |
tableChanged(javax.swing.event.TableModelEvent tableModelEvent)
This will be called by the underlying table model whenever the contents of the table are changed. |
Methods inherited from class org.afcs.warts.gui.TableMap |
getColumnClass, getColumnCount, getColumnName, getModel, getRowCount, isCellEditable |
Methods inherited from class javax.swing.table.AbstractTableModel |
addTableModelListener, findColumn, fireTableCellUpdated, fireTableChanged, fireTableDataChanged, fireTableRowsDeleted, fireTableRowsInserted, fireTableRowsUpdated, fireTableStructureChanged, getListeners, getTableModelListeners, removeTableModelListener |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
public TableSorter(javax.swing.table.TableModel model)
model
- The model providing the unsorted data.Method Detail |
public int getOriginalRowIndex(int rowIndex)
rowIndex
- The row index in the sorted model to look up.
java.lang.IllegalArgumentException
- If rowIndex is out of bounds.public int getSortColumn()
public java.lang.Object getValueAt(int rowIndex, int columnIndex)
getValueAt
in interface javax.swing.table.TableModel
getValueAt
in class TableMap
rowIndex
- The row to return data from.columnIndex
- The column to return data from.
public boolean isSortAscending()
public void listenTo(javax.swing.JTable table)
table
- The table to listen to.
java.lang.NullPointerException
- If table is null.public void setValueAt(java.lang.Object value, int rowIndex, int columnIndex)
setValueAt
in interface javax.swing.table.TableModel
setValueAt
in class TableMap
value
- The new value.rowIndex
- The row to set data in.columnIndex
- The column to set data in.public void sort(int columnIndex, boolean sortAscending)
columnIndex
- The column to sort on.sortAscending
- True if the sort should be ascending.
java.lang.IllegalArgumentException
- If columnIndex is out of bounds.public void tableChanged(javax.swing.event.TableModelEvent tableModelEvent)
tableChanged
in interface javax.swing.event.TableModelListener
tableChanged
in class TableMap
tableModelEvent
- Details of the event.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |