mckay.utilities.gui.tables
Class TableSortModel

java.lang.Object
  extended by mckay.utilities.gui.tables.TableSortModel
All Implemented Interfaces:
java.util.EventListener, javax.swing.event.TableModelListener, javax.swing.table.TableModel

public class TableSortModel
extends java.lang.Object
implements javax.swing.table.TableModel, javax.swing.event.TableModelListener

This class serves as an intermediate TableModel that can be used to implement sorting of JTables with DefaultTableModel TableModels.

The setColumnWidths method also allows JTables to have the width of some of their columns automatically set in order to fit the contents of the columns.


Constructor Summary
TableSortModel(javax.swing.JTable table)
          Attaches a MouseListener to the column headings of the given JTable so that it will be sorted based on the contents of that column whenever the column heading is pressed.
 
Method Summary
 void addRow(java.lang.Object[] row_data)
          Adds a row to the end of the TableModel.
 void addTableModelListener(javax.swing.event.TableModelListener listener)
          Adds the given listener to the table model.
 java.lang.Class getColumnClass(int column)
          Returns the class of the column at the given index.
 int getColumnCount()
          Returns the number of columns in the table.
 java.lang.String getColumnName(int column)
          Returns the name of the column at the given index.
 int getRowCount()
          Returns the number of rows in the table.
 int getTrueRowIndex(int row)
          Returns the row index of the true table model that corresponds to the row index in the sorted table model.
 java.lang.Object getValueAt(int row, int column)
          Returns the corresponding contents of the real TableModel.
 boolean isCellEditable(int row, int column)
          Returns the whether the given cell is editable or not.
 void removeRows(int[] row_indexes)
          Removes the rows with the given indexes from the TableModel.
 void removeTableModelListener(javax.swing.event.TableModelListener listener)
          Removes the given listener to the table model.
static void setColumnWidths(javax.swing.JTable table, javax.swing.table.TableColumn[] columns_to_set, int max)
          Resets the column widths of the given JTable so that each of the given columns has enough space to fit its header and all of its contents in the column, and no more.
 void setValueAt(java.lang.Object value, int row, int column)
          Sets the corresponding contents of the real TableModel.
 void tableChanged(javax.swing.event.TableModelEvent event)
          This method currently does nothing.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TableSortModel

public TableSortModel(javax.swing.JTable table)
               throws java.lang.IllegalArgumentException
Attaches a MouseListener to the column headings of the given JTable so that it will be sorted based on the contents of that column whenever the column heading is pressed. This object is attached to the table as an intermediate TableModel between it and its original DefaultTableModel.

Parameters:
table - The table that this object is to serve as an intermediateTableModel for. This may not have a null TableModel when it is passed here, and its TableModel must be a DefaultTableModel.
Throws:
java.lang.IllegalArgumentException - Throws an informative exception if a null argument is passed.
Method Detail

tableChanged

public void tableChanged(javax.swing.event.TableModelEvent event)
This method currently does nothing. Any code added to this method will automatically be executed whenever the table is changed.

Specified by:
tableChanged in interface javax.swing.event.TableModelListener
Parameters:
event - Any TableModelEvent that could trigger some action.

setColumnWidths

public static void setColumnWidths(javax.swing.JTable table,
                                   javax.swing.table.TableColumn[] columns_to_set,
                                   int max)
Resets the column widths of the given JTable so that each of the given columns has enough space to fit its header and all of its contents in the column, and no more. No column will be sized larger than the space needed to fit roughly the number of characters specified in the max argument, however.

Parameters:
table - The table whose column widths are to be changed.
columns_to_set - The columns to be resized. These must all be in the given table.
max - The rough maximum width of each of the given columns, in characters.

getTrueRowIndex

public int getTrueRowIndex(int row)
Returns the row index of the true table model that corresponds to the row index in the sorted table model.

Parameters:
row - The row index of the TableSortModel to query.
Returns:
The corresponding row index of the true TableModel.

addRow

public void addRow(java.lang.Object[] row_data)
Adds a row to the end of the TableModel.

Parameters:
row_data - The row to add to the TableModel.

removeRows

public void removeRows(int[] row_indexes)
Removes the rows with the given indexes from the TableModel.

Parameters:
row_indexes - The indexes of the rows to delete.

getValueAt

public java.lang.Object getValueAt(int row,
                                   int column)
Returns the corresponding contents of the real TableModel.

Specified by:
getValueAt in interface javax.swing.table.TableModel
Parameters:
row - The index of the sorted table.
column - The index of the column.
Returns:
The contents of the real TableModel at the given column and the corresponding row of the real TableModel.

setValueAt

public void setValueAt(java.lang.Object value,
                       int row,
                       int column)
Sets the corresponding contents of the real TableModel.

Specified by:
setValueAt in interface javax.swing.table.TableModel
Parameters:
value - The entry to place in the table.
row - The index of the sorted table.
column - The index of the column.

getRowCount

public int getRowCount()
Returns the number of rows in the table.

Specified by:
getRowCount in interface javax.swing.table.TableModel
Returns:
The number of rows in the table.

getColumnCount

public int getColumnCount()
Returns the number of columns in the table.

Specified by:
getColumnCount in interface javax.swing.table.TableModel
Returns:
The number of columns in the table.

getColumnName

public java.lang.String getColumnName(int column)
Returns the name of the column at the given index.

Specified by:
getColumnName in interface javax.swing.table.TableModel
Parameters:
column - The index of the column.
Returns:
The name of the column at the given column index.

getColumnClass

public java.lang.Class getColumnClass(int column)
Returns the class of the column at the given index.

Specified by:
getColumnClass in interface javax.swing.table.TableModel
Parameters:
column - The index of the column.
Returns:
The class of the column at the column index.

isCellEditable

public boolean isCellEditable(int row,
                              int column)
Returns the whether the given cell is editable or not.

Specified by:
isCellEditable in interface javax.swing.table.TableModel
Parameters:
row - The index of the sorted table.
column - The index of the column.
Returns:
Whether the given cell is editable.

addTableModelListener

public void addTableModelListener(javax.swing.event.TableModelListener listener)
Adds the given listener to the table model.

Specified by:
addTableModelListener in interface javax.swing.table.TableModel
Parameters:
listener - The listener to add.

removeTableModelListener

public void removeTableModelListener(javax.swing.event.TableModelListener listener)
Removes the given listener to the table model.

Specified by:
removeTableModelListener in interface javax.swing.table.TableModel
Parameters:
listener - The listener to remove from the table model.