|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectjavax.swing.table.AbstractTableModel
biggests.tables.TableSorter
public class TableSorter
Title: Table Sorter - a model for sorting tables
Description: TableSorter is a decorator for TableModels adding
sorting functionality to a supplied TableModel.
TableSorter does not store or copy the data in its
TableModel; instead it maintains a map from the row
indexes of the view to the row indexes of the model.
As requests are made of the sorter (like
getValueAt(row, col)) they are passed to the underlying
model after the row numbers have been translated via
the internal mapping array. This way, the TableSorter
appears to hold another copy of the table with the rows
in a different order.
TableSorter registers itself as a listener to the
underlying model, just as the JTable itself would.
Events received 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:
Copyright: Copyright (C) 2007 Joana Gonçalves This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
Version of 02/27/04
| Field Summary | |
|---|---|
static int |
ASCENDING
The status that indicates that a specific column is sorted in ascending order. |
protected java.util.HashMap<java.lang.Class,java.util.Comparator> |
columnComparators
Hash map for the columns' comparators. |
static java.util.Comparator |
COMPARABLE_COMPARATOR
The comparator for specific comparable objects. |
static int |
DESCENDING
The status that indicates that a specific column is sorted in descending order. |
static java.util.Comparator |
LEXICAL_COMPARATOR
The lexical comparator. |
protected java.awt.event.MouseListener |
mouseListener
The mouse listener of the table. |
static int |
NOT_SORTED
The status that indicates that a specific column is not sorted. |
protected javax.swing.table.JTableHeader |
tableHeader
The header of the table. |
protected javax.swing.table.TableModel |
tableModel
The table model. |
| Fields inherited from class javax.swing.table.AbstractTableModel |
|---|
listenerList |
| Constructor Summary | |
|---|---|
TableSorter()
Creates a new TableSorter with default mouse and
table model listeners. |
|
TableSorter(javax.swing.table.TableModel tableModel)
Creates a new TableSorter with the given table model. |
|
TableSorter(javax.swing.table.TableModel tableModel,
javax.swing.table.JTableHeader tableHeader)
Creates a new TableSorter with the given table
model and header. |
|
| Method Summary | |
|---|---|
java.lang.Class<?> |
getColumnClass(int column)
Returns the class of the column with index column. |
int |
getColumnCount()
Returns the number of columns in the table model. |
java.lang.String |
getColumnName(int column)
Returns the name of the given column. |
protected java.util.Comparator |
getComparator(int column)
Returns the appropriate comparator for the objects in the given column. |
protected javax.swing.Icon |
getHeaderRendererIcon(int column,
int size)
Returns the Icon corresponding to the sorting status of the given column. |
int |
getRowCount()
Returns the number of rows in the table model. |
int |
getSortingStatus(int column)
Returns the sorting status of the given column:DESCENDING NOT_SORTED ASCENDING |
javax.swing.table.JTableHeader |
getTableHeader()
Returns the table header. |
javax.swing.table.TableModel |
getTableModel()
Returns the table model. |
java.lang.Object |
getValueAt(int row,
int column)
Returns the element in the cell at row and
column indexes. |
boolean |
isCellEditable(int row,
int column)
Checks if the cell with the given row and
column indexes is editable. |
boolean |
isSorting()
Checks if the table is sorting. |
int |
modelIndex(int viewIndex)
Returns the model index for a given viewIndex. |
void |
setColumnComparator(java.lang.Class type,
java.util.Comparator comparator)
Sets the given comparator for the given objects'
type. |
void |
setSortingStatus(int column,
int status)
Sets the sorting status for a given column. |
void |
setTableHeader(javax.swing.table.JTableHeader tableHeader)
Sets the table header. |
void |
setTableModel(javax.swing.table.TableModel tableModel)
Sets the table model. |
void |
setValueAt(java.lang.Object aValue,
int row,
int column)
Sets the element of the cell with row and
column) indexes. |
| Methods inherited from class javax.swing.table.AbstractTableModel |
|---|
addTableModelListener, findColumn, fireTableCellUpdated, fireTableChanged, fireTableDataChanged, fireTableRowsDeleted, fireTableRowsInserted, fireTableRowsUpdated, fireTableStructureChanged, getListeners, getTableModelListeners, removeTableModelListener |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
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_COMPARATOR
public static final java.util.Comparator LEXICAL_COMPARATOR
protected javax.swing.table.JTableHeader tableHeader
protected java.awt.event.MouseListener mouseListener
protected java.util.HashMap<java.lang.Class,java.util.Comparator> columnComparators
| Constructor Detail |
|---|
public TableSorter()
TableSorter with default mouse and
table model listeners.
public TableSorter(javax.swing.table.TableModel tableModel)
TableSorter with the given table model.
tableModel - TableModel the model for this table
public TableSorter(javax.swing.table.TableModel tableModel,
javax.swing.table.JTableHeader tableHeader)
TableSorter with the given table
model and header.
tableModel - the TableModeltableHeader - the JTableHeader| Method Detail |
|---|
public javax.swing.table.TableModel getTableModel()
public void setTableModel(javax.swing.table.TableModel tableModel)
tableModel - the TableModelpublic javax.swing.table.JTableHeader getTableHeader()
public void setTableHeader(javax.swing.table.JTableHeader tableHeader)
tableHeader - the new JTableHeaderpublic boolean isSorting()
true if table is sorting,
false otherwise.
true if table is sorting,
false otherwisepublic int getSortingStatus(int column)
column:
column - int the index of the column
public void setSortingStatus(int column,
int status)
column.
column - int the column to set sorting statusstatus - int the new sorting status for the given column
protected javax.swing.Icon getHeaderRendererIcon(int column,
int size)
column. The icon will have the specified
size.
column - int the column to return the sorting
status icon forsize - int the size of the sorting status icon
public void setColumnComparator(java.lang.Class type,
java.util.Comparator comparator)
comparator for the given objects'
type.
type - the Class of objects to use the given
comparator withcomparator - the appropriate Comparator for
the objects of the specified typeprotected java.util.Comparator getComparator(int column)
column.
column - int the column for which the appropriate
comparator should be returned
columnpublic int modelIndex(int viewIndex)
viewIndex.
viewIndex - int the index that is shown in
the table
public int getRowCount()
public int getColumnCount()
public java.lang.String getColumnName(int column)
column.
getColumnName in interface javax.swing.table.TableModelgetColumnName in class javax.swing.table.AbstractTableModelcolumn - int the index of the intended column
public java.lang.Class<?> getColumnClass(int column)
column.Class to Class> so
this method overrides AbstractTableModel's
getColumnClass method (by Joana Gonçalves).
getColumnClass in interface javax.swing.table.TableModelgetColumnClass in class javax.swing.table.AbstractTableModelcolumn - int value: the column being queried
Class of the column at index column
public boolean isCellEditable(int row,
int column)
row and
column indexes is editable.
isCellEditable in interface javax.swing.table.TableModelisCellEditable in class javax.swing.table.AbstractTableModelrow - int the row index of the cellcolumn - int the column index of the cell
true if the cell at (row,
column) is editable, false otherwise
public java.lang.Object getValueAt(int row,
int column)
row and
column indexes.
row - int the row index of the cellcolumn - int the column index of the cell
row,
column)
public void setValueAt(java.lang.Object aValue,
int row,
int column)
row and
column) indexes.
setValueAt in interface javax.swing.table.TableModelsetValueAt in class javax.swing.table.AbstractTableModelaValue - Object the new element for the cellrow - int the row index of the cellcolumn - int the column index of the cell
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||