public class TableSorter
extends javax.swing.table.AbstractTableModel
TableSorter registers itself as a listener to the underlying model, just as the JTable itself would. Events recieved from the model are examined, sometimes manipulated (typically widened), and then passed on to the TableSorter's listeners (typically the JTable). If a change to the model has invalidated the order of TableSorter's rows, a note of this is made and the sorter will resort the rows the next time a value is requested.
When the tableHeader property is set, either by using the setTableHeader() method or the two argument constructor, the table header may be used as a complete UI for TableSorter. The default renderer of the tableHeader is decorated with a renderer that indicates the sorting status of each column. In addition, a mouse listener is installed with the following behavior:
This is a long overdue rewrite of a class of the same name that first appeared in the swing table demos in 1997.
Modifier and Type | Field and Description |
---|---|
static int |
ASCENDING |
static java.util.Comparator |
COMPARABLE_COMAPRATOR |
static int |
DESCENDING |
static java.util.Comparator |
LEXICAL_COMPARATOR |
static int |
NOT_SORTED |
protected javax.swing.table.TableModel |
tableModel |
Constructor and Description |
---|
TableSorter() |
TableSorter(javax.swing.table.TableModel tableModel) |
TableSorter(javax.swing.table.TableModel tableModel,
javax.swing.table.JTableHeader tableHeader) |
Modifier and Type | Method and Description |
---|---|
java.lang.Class |
getColumnClass(int column) |
int |
getColumnCount() |
java.lang.String |
getColumnName(int column) |
protected java.util.Comparator |
getComparator(int column) |
protected javax.swing.Icon |
getHeaderRendererIcon(int column,
int size) |
int |
getRowCount() |
int |
getSortingStatus(int column) |
javax.swing.table.JTableHeader |
getTableHeader() |
javax.swing.table.TableModel |
getTableModel() |
java.lang.Object |
getValueAt(int row,
int column) |
boolean |
isCellEditable(int row,
int column) |
boolean |
isSorting() |
int |
modelIndex(int viewIndex) |
void |
setColumnComparator(java.lang.Class type,
java.util.Comparator comparator) |
void |
setSortingStatus(int column,
int status) |
void |
setTableHeader(javax.swing.table.JTableHeader tableHeader) |
void |
setTableModel(javax.swing.table.TableModel tableModel) |
void |
setValueAt(java.lang.Object aValue,
int row,
int column) |
int |
viewIndex(int modelIndex) |
addTableModelListener, findColumn, fireTableCellUpdated, fireTableChanged, fireTableDataChanged, fireTableRowsDeleted, fireTableRowsInserted, fireTableRowsUpdated, fireTableStructureChanged, getListeners, getTableModelListeners, removeTableModelListener
protected javax.swing.table.TableModel tableModel
public static final int DESCENDING
public static final int NOT_SORTED
public static final int ASCENDING
public static final java.util.Comparator COMPARABLE_COMAPRATOR
public static final java.util.Comparator LEXICAL_COMPARATOR
public TableSorter()
public TableSorter(javax.swing.table.TableModel tableModel)
public TableSorter(javax.swing.table.TableModel tableModel, javax.swing.table.JTableHeader tableHeader)
public javax.swing.table.TableModel getTableModel()
public void setTableModel(javax.swing.table.TableModel tableModel)
public javax.swing.table.JTableHeader getTableHeader()
public void setTableHeader(javax.swing.table.JTableHeader tableHeader)
public boolean isSorting()
public int getSortingStatus(int column)
public void setSortingStatus(int column, int status)
protected javax.swing.Icon getHeaderRendererIcon(int column, int size)
public void setColumnComparator(java.lang.Class type, java.util.Comparator comparator)
protected java.util.Comparator getComparator(int column)
public int modelIndex(int viewIndex)
public int viewIndex(int modelIndex)
public int getRowCount()
public int getColumnCount()
public java.lang.String getColumnName(int column)
getColumnName
in interface javax.swing.table.TableModel
getColumnName
in class javax.swing.table.AbstractTableModel
public java.lang.Class getColumnClass(int column)
getColumnClass
in interface javax.swing.table.TableModel
getColumnClass
in class javax.swing.table.AbstractTableModel
public boolean isCellEditable(int row, int column)
isCellEditable
in interface javax.swing.table.TableModel
isCellEditable
in class javax.swing.table.AbstractTableModel
public java.lang.Object getValueAt(int row, int column)
public void setValueAt(java.lang.Object aValue, int row, int column)
setValueAt
in interface javax.swing.table.TableModel
setValueAt
in class javax.swing.table.AbstractTableModel