public class TableSorter extends ProxyTableModel
TableModel
s. 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.Modifier and Type | Field and Description |
---|---|
private boolean |
ascending |
private int |
compares |
private int[] |
indexes |
private boolean |
sortedAscending |
private int |
sortedColumn |
private Vector |
sortingColumns |
private JTable |
tableView |
model
listenerList
Constructor and Description |
---|
TableSorter()
Construct a new
TableSorter . |
TableSorter(TableModel model)
Construct a new
TableSorter with a specified data model.. |
Modifier and Type | Method and Description |
---|---|
private void |
checkModel() |
private int |
compare(int row1,
int row2) |
private int |
compareRowsByColumn(int row1,
int row2,
int column) |
int |
getReverseRowTranslation(int row)
Return the visible index of the given row in the unsorted model.
|
int |
getRowTranslation(int row)
Return the index of the given row in the unsorted model that this
model wraps.
|
int |
getSortedColumn()
Get the index of the last column that the table was sorted on.
|
Object |
getValueAt(int row,
int col)
Get the value at the given row and column of the unsorted table.
|
boolean |
isSortedAscending()
Determine if the last column sort was ascending or descending.
|
private void |
reallocateIndexes() |
void |
registerTableHeaderListener(JTable table)
Add a mouse listener to the
JTable to trigger a table sort
when a column heading is clicked. |
void |
setModel(TableModel model)
Set the
TableModel for this table sorter. |
void |
setValueAt(Object value,
int row,
int col)
Set the value at the given row and column of the unsorted table.
|
private void |
shuttlesort(int[] from,
int[] to,
int low,
int high) |
private void |
sort(Object sender) |
void |
sortByColumn(int column)
Sort a column in the table in ascending order.
|
void |
sortByColumn(int column,
boolean ascending)
Sort a column in the table.
|
private void |
swap(int i,
int j) |
void |
tableChanged(TableModelEvent e)
Handle table changed events.
|
getColumnClass, getColumnCount, getColumnName, getModel, getRowCount, isCellEditable
addTableModelListener, findColumn, fireTableCellUpdated, fireTableChanged, fireTableDataChanged, fireTableRowsDeleted, fireTableRowsInserted, fireTableRowsUpdated, fireTableStructureChanged, getListeners, getTableModelListeners, removeTableModelListener
private int[] indexes
private Vector sortingColumns
private boolean ascending
private int compares
private int sortedColumn
private boolean sortedAscending
public TableSorter()
TableSorter
.public TableSorter(TableModel model)
TableSorter
with a specified data model..model
- The TableModel
to use.public int getRowTranslation(int row)
getReverseRowTranslation(int)
public int getReverseRowTranslation(int row)
getRowTranslation()
getRowTranslation(int)
public void setModel(TableModel model)
TableModel
for this table sorter.setModel
in class ProxyTableModel
model
- The TableModel
to use.public Object getValueAt(int row, int col)
getValueAt
in interface TableModel
getValueAt
in class ProxyTableModel
row
- The row.col
- The column.public void setValueAt(Object value, int row, int col)
setValueAt
in interface TableModel
setValueAt
in class ProxyTableModel
row
- The row.col
- The column.value
- The new value.public void sortByColumn(int column)
column
- The index of the column to sort.public void sortByColumn(int column, boolean ascending)
column
- The index of the column to sort.ascending
- If true
, sorts in ascending order;
otherwise, sorts in descending order.public int getSortedColumn()
public boolean isSortedAscending()
true
if the sort was ascending, and
false
otherwise. This value is only meaningful if
getSortedColumn()
returns a non-negative value.public void tableChanged(TableModelEvent e)
tableChanged
in interface TableModelListener
tableChanged
in class ProxyTableModel
public void registerTableHeaderListener(JTable table)
JTable
to trigger a table sort
when a column heading is clicked. A shift click causes the column to
be sorted in descending order, whereas a simple click causes the column
to be sorted in ascending order.table
- The JTable
to listen for events on.private int compareRowsByColumn(int row1, int row2, int column)
private int compare(int row1, int row2)
private void reallocateIndexes()
private void checkModel()
private void shuttlesort(int[] from, int[] to, int low, int high)
private void swap(int i, int j)
WebARTS Library Licensed Under the GNU - General Public License. Other Libraries licensed under their respective Open Source Licenses