public class SortableTableModelProxy extends Object implements ExtendedSortableTableModel, TableModelListener, Serializable
TableModel, and allows to sort it by one
of its columns. Other TableModel calls are forwarded to it.
The sorting of the rows takes place in this proxy only. The master TableModel
is not changed in any way by the sorting procedure. You should keep in mind though,
that the rows will be re-sorted after every single manipulation to the master
TableModel.
The liaison between the SortableTableModel and its master TableModel is
for a lifetime. If you want to change the master TableModel, you will have to
generate a new SortTableModel and feed it to the JTable. This
TableModel is best suited for the JSortedTable, but not limited to it.
You can also use SortableTableModelProxy instances for classic JTable,
if you want to keep control of the column sorting.
Note that this is just a proxy, not a TableModel itself. You should not extend
this class in order to create a sorted TableModel, but inherit from standard
TableModels, and then pass your TableModel to the SortTableModelProxy
constructor.
| Constructor and Description |
|---|
SortableTableModelProxy(TableModel master)
Creates a new SortableTableModelProxy for a master
TableModel. |
| Modifier and Type | Method and Description |
|---|---|
void |
addTableModelListener(TableModelListener l)
Adds a
TableModelListener. |
protected void |
fireTableDataChanged()
Notifies all
TableModelListener that the sort order was changed. |
Class<?> |
getColumnClass(int columnIndex) |
int |
getColumnCount() |
String |
getColumnName(int columnIndex) |
TableModel |
getMasterTable()
Gets the master table that is connected to this SortableTableModelProxy.
|
int |
getRowCount() |
int |
getSortedColumn()
Gets the index of the column that is currently sorted.
|
Object |
getValueAt(int rowIndex,
int columnIndex) |
boolean |
isCellEditable(int rowIndex,
int columnIndex) |
boolean |
isColumnSortable(int columnIndex)
Returns
true if a column is sortable. |
boolean |
isDescending()
Gets the current sort order.
|
int |
mapRow(int row)
Maps a row number to the appropriate row number in the master
TableModel. |
protected void |
rebuildIndexMap()
Rebuilds the internal index map array.
|
void |
removeTableModelListener(TableModelListener l)
Removes a
TableModelListener. |
protected void |
resort()
Sorts the entire TableModel again.
|
void |
setValueAt(Object aValue,
int rowIndex,
int columnIndex) |
void |
sortByColumn(int columnIndex)
Sorts by a certain column, ascending.
|
void |
sortByColumn(int columnIndex,
boolean desc)
Sorts by a certain column in the given order.
|
void |
sortByColumnDesc(int columnIndex)
Sorts by a certain column, descending.
|
void |
tableChanged(TableModelEvent e)
Internal TableModelListener implementation, do not use!
|
int |
unmapRow(int row)
Maps a row number of the master
TableModel to the current row number of the
SortableTableModel. |
public SortableTableModelProxy(TableModel master)
TableModel. The object
will give a modified view to the master TableModel that allows to sort it
by a certain column.master - Master TableModelpublic TableModel getMasterTable()
TableModelpublic int getRowCount()
getRowCount in interface TableModelpublic int getColumnCount()
getColumnCount in interface TableModelpublic String getColumnName(int columnIndex)
getColumnName in interface TableModelpublic Class<?> getColumnClass(int columnIndex)
getColumnClass in interface TableModelpublic boolean isCellEditable(int rowIndex, int columnIndex)
isCellEditable in interface TableModelpublic Object getValueAt(int rowIndex, int columnIndex)
getValueAt in interface TableModelpublic void setValueAt(Object aValue, int rowIndex, int columnIndex)
setValueAt in interface TableModelpublic int mapRow(int row)
TableModel.row - Row number of this modelTableModel.public int unmapRow(int row)
TableModel to the current row number of the
SortableTableModel.row - Row number of the master TableModelpublic void sortByColumn(int columnIndex)
columnIndex - Column to sort at.public void sortByColumnDesc(int columnIndex)
columnIndex - Column to sort at.public void sortByColumn(int columnIndex, boolean desc)
sortByColumn in interface SortableTableModelcolumnIndex - Column to sort at.desc - true: descending, false: ascendingpublic int getSortedColumn()
getSortedColumn in interface SortableTableModelpublic boolean isDescending()
isDescending in interface SortableTableModelprotected void resort()
protected void rebuildIndexMap()
public void tableChanged(TableModelEvent e)
tableChanged in interface TableModelListenerpublic void addTableModelListener(TableModelListener l)
TableModelListener. It will be added to the master
TableModel as well as to this SortTableModelProxy. The listener will be
notified whenever the master TableModel, or the column to be sorted, was
changed.addTableModelListener in interface TableModell - TableModelListenerpublic void removeTableModelListener(TableModelListener l)
TableModelListener. If it was not added, nothing will happen.
Make sure that you use the proxy
removeTableModelListener(TableModelListener) when you have used the proxy
addTableModelListener(TableModelListener).removeTableModelListener in interface TableModell - TableModelListenerprotected void fireTableDataChanged()
TableModelListener that the sort order was changed.public boolean isColumnSortable(int columnIndex)
true if a column is sortable. This method always returns
true. It can be overridden to avoid sorting of different columns.isColumnSortable in interface ExtendedSortableTableModelcolumnIndex - Column index to checktrueCopyright © 2004–2016. All rights reserved.