net.sf.swinglib.table
Class ExpandingTableModel

java.lang.Object
  extended by javax.swing.table.AbstractTableModel
      extended by net.sf.swinglib.table.ExpandingTableModel
All Implemented Interfaces:
Serializable, TableModel

public class ExpandingTableModel
extends AbstractTableModel

A TableModel that can be resized on the fly, tracks whether a cell has been changed, and allows the table to add new rows via edits.

The "expanding" behavior is supported by reporting an extra row via getRowCount(). Calls to getValueAt(int, int) for this "phantom" row return null, while calls to setValueAt(java.lang.Object, int, int) make the row permanent and create a new phantom row.

See Also:
Serialized Form

Field Summary
 
Fields inherited from class javax.swing.table.AbstractTableModel
listenerList
 
Constructor Summary
ExpandingTableModel()
          Creates an instance that has no rows and no columns.
ExpandingTableModel(int rows, int cols)
          Creates an instance that is pre-sized to the specified number of rows and columns, with all cells containing null.
ExpandingTableModel(Object[][] data)
          Creates an instance that is initialized with the passed data.
ExpandingTableModel(Object[][] data, Object[] columnNames)
          Creates an instance that is initialized with the passed data and column headers.
ExpandingTableModel(Object[][] data, Object[] columnNames, Class<?>[] colClasses)
          Creates an instance that is initialized with the passed data, column headers, and column classes.
 
Method Summary
 Class<?> getColumnClass(int col)
          Returns the class of the particular column, Object if no class has been specified for the column.
 int getColumnCount()
          Returns the current number of columns in the model.
 String getColumnName(int col)
          Returns the name of the particular column, empty string if a header has not been defined for the column.
 int getRowCount()
          Returns the number of rows in the model, including the "phantom" row.
 Object getValueAt(int row, int col)
          Returns the value at a particular cell in the model.
 boolean isCellEditable(int row, int col)
           
 void setColumnClass(int col, Class<?> klass)
          Updates the name of a column with the passed value.
 void setColumnName(int col, Object name)
          Updates the name for a column with the passed value.
 void setValueAt(Object value, int row, int col)
          Stores the passed value in the specified cell.
 void setWidth(int width)
          Sets the width (column count) of the model.
 
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
 

Constructor Detail

ExpandingTableModel

public ExpandingTableModel()
Creates an instance that has no rows and no columns. Application must at least call setWidth(int).


ExpandingTableModel

public ExpandingTableModel(int rows,
                           int cols)
Creates an instance that is pre-sized to the specified number of rows and columns, with all cells containing null.


ExpandingTableModel

public ExpandingTableModel(Object[][] data)
Creates an instance that is initialized with the passed data. The passed rows may be different length: the model will size itself to the largest and fill missing values with null.


ExpandingTableModel

public ExpandingTableModel(Object[][] data,
                           Object[] columnNames)
Creates an instance that is initialized with the passed data and column headers. The number of headers and number of columns need not match: excess headers will be retained for later use, insufficient headers are returned as "".

Note that headers are defined as arbitrary objects. If the object is mutable, the header value will reflect the string value of the header.


ExpandingTableModel

public ExpandingTableModel(Object[][] data,
                           Object[] columnNames,
                           Class<?>[] colClasses)
Creates an instance that is initialized with the passed data, column headers, and column classes. The number of headers/classes and number of columns need not match: excess headers/classes will be retained for later use, insufficient headers/classes are returned as "" and Object respectively.

Throws:
IllegalArgumentException - if a data element does not correspond to the class specification.
Method Detail

getColumnCount

public int getColumnCount()
Returns the current number of columns in the model. This may be changed via setWidth(int).


getRowCount

public int getRowCount()
Returns the number of rows in the model, including the "phantom" row.


getColumnName

public String getColumnName(int col)
Returns the name of the particular column, empty string if a header has not been defined for the column.

Note that headers may be specified as any type of object, but are returned as strings. This is no doubt an artifact of the original implementation using Vector, but allows the creation of dynamic headers using mutable objects.

Specified by:
getColumnName in interface TableModel
Overrides:
getColumnName in class AbstractTableModel

getColumnClass

public Class<?> getColumnClass(int col)
Returns the class of the particular column, Object if no class has been specified for the column.

Specified by:
getColumnClass in interface TableModel
Overrides:
getColumnClass in class AbstractTableModel

getValueAt

public Object getValueAt(int row,
                         int col)
Returns the value at a particular cell in the model. Cells in the "phantom" row always return null

Throws:
IndexOutOfBoundsException - on attempts to get an invalid cell.

setValueAt

public void setValueAt(Object value,
                       int row,
                       int col)
Stores the passed value in the specified cell. If this cell is in the "phantom" row, will make that row permanent (and increase the size of the table).

Specified by:
setValueAt in interface TableModel
Overrides:
setValueAt in class AbstractTableModel
Throws:
IndexOutOfBoundsException - on attempts to set an invalid cell.

isCellEditable

public boolean isCellEditable(int row,
                              int col)
Specified by:
isCellEditable in interface TableModel
Overrides:
isCellEditable in class AbstractTableModel

setWidth

public void setWidth(int width)
Sets the width (column count) of the model. Does nothing if called with the current width. Will remove elements from the end of each row if the specified width is less than the current width, insert nulls on each row if greater.

Header names are not removed when the width is reduced, and will re-appear if the table is subsequently widened.


setColumnName

public void setColumnName(int col,
                          Object name)
Updates the name for a column with the passed value. Names may be set for columns that are not yet shown; they will become active when the table is widened.


setColumnClass

public void setColumnClass(int col,
                           Class<?> klass)
Updates the name of a column with the passed value. Classes may be set for columns that are not yet shown; they will become active when the table is widened.