public class TableSorter
extends TableMap
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 algorithm used is stable which means that it does not move around rows when its comparison function returns 0 to denote that they are equivalent.
| Constructor and description |
|---|
TableSorter()Creates an empty sorter with no delegate model. |
TableSorter(TableModel model)Creates a sorter for the supplied table model. |
| Type Params | Return Type | Name and description |
|---|---|---|
|
public void |
addMouseListenerToHeaderInTable(JTable table)Installs a header listener that resorts the table when a column header is clicked. |
|
public void |
checkModel()Verifies that the cached index mapping still matches the delegate model. |
|
public int |
compare(int row1, int row2)Compares two source rows using the active sort columns. |
|
public int |
compareRowsByColumn(int row1, int row2, int column)Compares two source rows using the values from the supplied column. |
|
public Object |
getValueAt(int aRow, int aColumn)Returns the value from the source row mapped to the supplied view row. |
|
public void |
n2sort()Sorts the row mapping using a simple quadratic algorithm. |
|
public void |
reallocateIndexes()Rebuilds the row index mapping to match the current delegate row count. |
|
public void |
setModel(TableModel model)Replaces the wrapped model and rebuilds the cached row index mapping. |
|
public void |
setValueAt(Object aValue, int aRow, int aColumn)Writes a value to the source row mapped from the supplied view row. |
|
public void |
shuttlesort(int[] from, int[] to, int low, int high)Performs a stable merge sort over the cached row mapping. |
|
public void |
sort(Object sender)Sorts the cached row mapping using the currently configured sort columns. |
|
public void |
sortByColumn(int column)Sorts the table by the supplied model column in ascending order. |
|
public void |
sortByColumn(int column, boolean ascending)Sorts the table by the supplied model column and direction. |
|
public void |
swap(int i, int j)Swaps two entries in the cached row mapping. |
|
public void |
tableChanged(TableModelEvent e)Rebuilds the cached row mapping after any delegate table-model change. |
| Methods inherited from class | Name |
|---|---|
class TableMap |
getColumnClass, getColumnCount, getColumnName, getModel, getRowCount, getValueAt, isCellEditable, setModel, setValueAt, tableChanged |
Creates an empty sorter with no delegate model.
Creates a sorter for the supplied table model.
model - the model to wrapInstalls a header listener that resorts the table when a column header is clicked.
table - the table whose header should trigger sortingVerifies that the cached index mapping still matches the delegate model.
Compares two source rows using the active sort columns.
row1 - the first source row indexrow2 - the second source row indexCompares two source rows using the values from the supplied column.
row1 - the first source row indexrow2 - the second source row indexcolumn - the model column index to compareReturns the value from the source row mapped to the supplied view row.
aRow - the sorted view rowaColumn - the model columnSorts the row mapping using a simple quadratic algorithm.
Rebuilds the row index mapping to match the current delegate row count.
Replaces the wrapped model and rebuilds the cached row index mapping.
model - the model to sortWrites a value to the source row mapped from the supplied view row.
aValue - the new cell valueaRow - the sorted view rowaColumn - the model columnPerforms a stable merge sort over the cached row mapping.
from - the source index arrayto - the target index arraylow - the inclusive lower boundhigh - the exclusive upper boundSorts the cached row mapping using the currently configured sort columns.
sender - the caller requesting the sortSorts the table by the supplied model column in ascending order.
column - the model column index to sort bySorts the table by the supplied model column and direction.
column - the model column index to sort byascending - true for ascending order, false for descending orderSwaps two entries in the cached row mapping.
i - the first mapped row indexj - the second mapped row indexRebuilds the cached row mapping after any delegate table-model change.
e - the table-model eventCopyright © 2003-2026 The Apache Software Foundation. All rights reserved.