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 | Class and Description |
|---|---|
| private static class | |
| private static class | |
| private class | |
| private class | |
| private class | |
| private class |
| Modifier and Type | Field and Description |
|---|---|
| public static final int | |
| private Map | |
| public static final Comparator | |
| public static final int | |
| private static TableSorter. | |
| public static final Comparator | |
| private int[] | |
| private MouseListener | |
| public static final int | |
| private List | |
| private JTableHeader | |
| protected TableModel | |
| private TableModelListener | |
| private TableSorter. |
| Access | Constructor and Description |
|---|---|
| public | |
| public | |
| public |
| Modifier and Type | Method and Description |
|---|---|
| private void | |
| private void | |
| public Class | getColumnClass(int column)
Overrides javax. Implements javax. Returns |
| public int | getColumnCount()
Implements javax. Returns the number of columns in the model. |
| public String | getColumnName(int column)
Overrides javax. Implements javax. Returns a default name for the column using spreadsheet conventions: A, B, C, ... |
| protected Comparator | |
| private TableSorter. | |
| protected Icon | |
| private int[] | |
| public int | getRowCount()
Implements javax. Returns the number of rows in the model. |
| public int | |
| public JTableHeader | |
| public TableModel | |
| public Object | getValueAt(int row, int column)
Implements javax. Returns the value for the cell at |
| private TableSorter. | |
| public boolean | isCellEditable(int row, int column)
Overrides javax. Implements javax. Returns false. |
| public boolean | |
| public int | |
| public void | |
| public void | |
| public void | |
| public void | |
| public void | setValueAt(Object aValue, int row, int column)
Overrides javax. Implements javax. This empty implementation is provided so users don't have to implement this method if their data model is not editable. |
| private void |
| ASCENDING | back to summary |
|---|---|
| public static final int ASCENDING | |
| columnComparators | back to summary |
|---|---|
| private Map<K, V> columnComparators | |
| COMPARABLE_COMPARATOR | back to summary |
|---|---|
| public static final Comparator<T> COMPARABLE_COMPARATOR | |
| DESCENDING | back to summary |
|---|---|
| public static final int DESCENDING | |
| EMPTY_DIRECTIVE | back to summary |
|---|---|
| private static TableSorter. | |
| LEXICAL_COMPARATOR | back to summary |
|---|---|
| public static final Comparator<T> LEXICAL_COMPARATOR | |
| modelToView | back to summary |
|---|---|
| private int[] modelToView | |
| mouseListener | back to summary |
|---|---|
| private MouseListener mouseListener | |
| NOT_SORTED | back to summary |
|---|---|
| public static final int NOT_SORTED | |
| sortingColumns | back to summary |
|---|---|
| private List<E> sortingColumns | |
| tableHeader | back to summary |
|---|---|
| private JTableHeader tableHeader | |
| tableModel | back to summary |
|---|---|
| protected TableModel tableModel | |
| tableModelListener | back to summary |
|---|---|
| private TableModelListener tableModelListener | |
| viewToModel | back to summary |
|---|---|
| private TableSorter. | |
| TableSorter | back to summary |
|---|---|
| public TableSorter() | |
| TableSorter | back to summary |
|---|---|
| public TableSorter(TableModel tableModel) | |
| TableSorter | back to summary |
|---|---|
| public TableSorter(TableModel tableModel, JTableHeader tableHeader) | |
| cancelSorting | back to summary |
|---|---|
| private void cancelSorting() | |
| clearSortingState | back to summary |
|---|---|
| private void clearSortingState() | |
| getColumnClass | back to summary |
|---|---|
| public Class Overrides javax. Implements javax. Doc from javax. Returns | |
| getColumnCount | back to summary |
|---|---|
| public int getColumnCount() Implements javax. Doc from javax. Returns the number of columns in the model. A
| |
| getColumnName | back to summary |
|---|---|
| public String getColumnName(int column) Overrides javax. Implements javax. Doc from javax. Returns a default name for the column using spreadsheet conventions:
A, B, C, ... Z, AA, AB, etc. If | |
| getComparator | back to summary |
|---|---|
| protected Comparator | |
| getDirective | back to summary |
|---|---|
| private TableSorter. | |
| getHeaderRendererIcon | back to summary |
|---|---|
| protected Icon getHeaderRendererIcon(int column, int size) | |
| getModelToView | back to summary |
|---|---|
| private int[] getModelToView() | |
| getRowCount | back to summary |
|---|---|
| public int getRowCount() Implements javax. Doc from javax. Returns the number of rows in the model. A
| |
| getSortingStatus | back to summary |
|---|---|
| public int getSortingStatus(int column) | |
| getTableHeader | back to summary |
|---|---|
| public JTableHeader getTableHeader() | |
| getTableModel | back to summary |
|---|---|
| public TableModel getTableModel() | |
| getValueAt | back to summary |
|---|---|
| public Object getValueAt(int row, int column) Implements javax. Doc from javax. Returns the value for the cell at | |
| getViewToModel | back to summary |
|---|---|
| private TableSorter. | |
| isCellEditable | back to summary |
|---|---|
| public boolean isCellEditable(int row, int column) Overrides javax. Implements javax. Doc from javax. Returns false. This is the default implementation for all cells. | |
| isSorting | back to summary |
|---|---|
| public boolean isSorting() | |
| modelIndex | back to summary |
|---|---|
| public int modelIndex(int viewIndex) | |
| setColumnComparator | back to summary |
|---|---|
| public void setColumnComparator(Class<T> type, Comparator<T> comparator) | |
| setSortingStatus | back to summary |
|---|---|
| public void setSortingStatus(int column, int status) | |
| setTableHeader | back to summary |
|---|---|
| public void setTableHeader(JTableHeader tableHeader) | |
| setTableModel | back to summary |
|---|---|
| public void setTableModel(TableModel tableModel) | |
| setValueAt | back to summary |
|---|---|
| public void setValueAt(Object aValue, int row, int column) Overrides javax. Implements javax. Doc from javax. This empty implementation is provided so users don't have to implement this method if their data model is not editable. | |
| sortingStatusChanged | back to summary |
|---|---|
| private void sortingStatusChanged() | |
| Modifier and Type | Field and Description |
|---|---|
| private boolean | |
| private int | |
| private int |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public int | |
| public int | |
| public void | paintIcon(Component c, Graphics g, int x, int y)
Implements javax. Draw the icon at the specified location. |
| descending | back to summary |
|---|---|
| private boolean descending | |
| priority | back to summary |
|---|---|
| private int priority | |
| size | back to summary |
|---|---|
| private int size | |
| Arrow | back to summary |
|---|---|
| public Arrow(boolean descending, int size, int priority) | |
| getIconHeight | back to summary |
|---|---|
| public int getIconHeight() Implements javax. Doc from javax. Returns the icon's height. | |
| getIconWidth | back to summary |
|---|---|
| public int getIconWidth() Implements javax. Doc from javax. Returns the icon's width. | |
| paintIcon | back to summary |
|---|---|
| public void paintIcon(Component c, Graphics g, int x, int y) Implements javax. Doc from javax. Draw the icon at the specified location. Icon implementations may use the Component argument to get properties useful for painting, e.g. the foreground or background color. | |
| Access | Constructor and Description |
|---|---|
| public |
| column | back to summary |
|---|---|
| private int column | |
| direction | back to summary |
|---|---|
| private int direction | |
| Directive | back to summary |
|---|---|
| public Directive(int column, int direction) | |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public void | mouseClicked(MouseEvent e)
Overrides java. Implements java. Invoked when the mouse button has been clicked (pressed and released) on a component. |
| MouseHandler | back to summary |
|---|---|
| private MouseHandler() | |
| mouseClicked | back to summary |
|---|---|
| public void mouseClicked(MouseEvent e) Overrides java. Implements java. Doc from java. Invoked when the mouse button has been clicked (pressed and released) on a component. | |
| Modifier and Type | Field and Description |
|---|---|
| private int |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public int | compareTo(Object o)
Implements java. Compares this object with the specified object for order. |
| modelIndex | back to summary |
|---|---|
| private int modelIndex | |
| Row | back to summary |
|---|---|
| public Row(int index) | |
| compareTo | back to summary |
|---|---|
| public int compareTo(Object o) Implements java. Doc from java. Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object. The implementor must ensure The implementor must also ensure that the relation is transitive:
Finally, the implementor must ensure that | |
| Modifier and Type | Field and Description |
|---|---|
| private TableCellRenderer |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public Component | getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
Implements javax. Returns the component used for drawing the cell. |
| tableCellRenderer | back to summary |
|---|---|
| private TableCellRenderer tableCellRenderer | |
| SortableHeaderRenderer | back to summary |
|---|---|
| public SortableHeaderRenderer(TableCellRenderer tableCellRenderer) | |
| getTableCellRendererComponent | back to summary |
|---|---|
| public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) Implements javax. Doc from javax. Returns the component used for drawing the cell. This method is used to configure the renderer appropriately before drawing.
The
JTable.DropLocation dropLocation = table.getDropLocation();
if (dropLocation != null
&& !dropLocation.isInsertRow()
&& !dropLocation.isInsertColumn()
&& dropLocation.getRow() == row
&& dropLocation.getColumn() == column) {
// this cell represents the current drop location
// so render it specially, perhaps with a different color
}
During a printing operation, this method will be called with
| |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public void | tableChanged(TableModelEvent e)
Implements javax. This fine grain notification tells listeners the exact range of cells, rows, or columns that changed. |
| TableModelHandler | back to summary |
|---|---|
| private TableModelHandler() | |
| tableChanged | back to summary |
|---|---|
| public void tableChanged(TableModelEvent e) Implements javax. Doc from javax. This fine grain notification tells listeners the exact range of cells, rows, or columns that changed. | |