net.sf.jga.swing
Class GenericTableModel<T>

java.lang.Object
  extended by javax.swing.table.AbstractTableModel
      extended by net.sf.jga.swing.GenericTableModel<T>
All Implemented Interfaces:
java.io.Serializable, javax.swing.table.TableModel

public class GenericTableModel<T>
extends javax.swing.table.AbstractTableModel

TableModel that uses a list of data for storage, and whose columns contain functors that read (and possibly write) properties of the objects in the list.

This class uses GenericTableColumns to store information about each specific column. To keep the correct column classes in use by the table, it is important to build the table using the constructor that takes both the data model and the column model, as shown in the code sample below. Otherwise, the table will build a default TableColumnModel of its own, and additions of columns to the data model won't be reflected in the column model.

Another caveat on the use of this class is that if columns are to be added to the model after it has been given to a table, then the table's autoCreateColumnsFromModel flag must be false (otherwise, the table will discard the existing columns and attempt to build new ones: JTable won't know how to create instances of the generic classes.

Working with this class can be fairly simple. To build a simple table that allows for the display and editing of a typical business object (Item, in this example),

 List data = // initialized from somewhere
 GenericTableModel model =
     new GenericTableModel(Item.class, data);

  // adds a read-only column for the object's id
  model.addColumn(Integer.class, "ID");
  
  // adds a read-only column for the object's name
  model.addColumn(String.class, "Name");
  
  // adds an editable column for the object's description
  model.addColumn(String.class, "Desc", true);

  // adds an editable column for the object's count
  model.addColumn(Integer.class, "Count", true);

  // adds an editable column for the object's price
  model.addColumn(BigDecimal.class, "Price", true);
 
 JTable table = new JTable(model, model.getColumnModel());
 

In this example, the Item class is presumed to have the appropriate getter/setter methods defined.

Copyright © 2003-2005 David A. Hall

Author:
David A. Hall
See Also:
Serialized Form

Field Summary
 
Fields inherited from class javax.swing.table.AbstractTableModel
listenerList
 
Constructor Summary
GenericTableModel(java.lang.Class<T> rowtype, java.util.List<T> values)
          Builds a GenericTableModel for the given list of data.
GenericTableModel(java.lang.Class<T> rowtype, java.util.List<T> values, java.util.List<GenericTableColumn<T,?>> columns)
          Builds a GenericTableModel for the given list of data, using the given list of columns.
 
Method Summary
<C> GenericTableColumn<T,C>
addColumn(java.lang.Class<C> coltype, java.lang.String name)
          Adds a read-only column that uses a GetProperty functor for the named property.
<C> GenericTableColumn<T,C>
addColumn(java.lang.Class<C> coltype, java.lang.String name, boolean editable)
          Adds a possibly editable column that uses a GetProperty functor for the named property, and a SetProperty functor if the column is editable.
<C> GenericTableColumn<T,C>
addColumn(java.lang.Class<C> coltype, java.lang.String name, UnaryFunctor<C,java.lang.String> formatter)
          Adds a read-only column that uses a GetProperty functor for the named property and renders the property value in the given formatter.
<C> GenericTableColumn<T,C>
addColumn(java.lang.Class<C> coltype, java.lang.String name, UnaryFunctor<C,java.lang.String> formatter, UnaryFunctor<java.lang.String,C> parser)
          Adds an editable column that uses a GetProperty functor for the named property, and a SetProperty functor if the column is editable, and is rendered (and edited) using the given format.
 void addColumn(GenericTableColumn<T,?> col)
          Adds the given column to the table.
 java.lang.Class<?> getColumnClass(int col)
           
 int getColumnCount()
           
 javax.swing.table.TableColumnModel getColumnModel()
          Returns the column model that should be used in conjunction with this data model.
 java.lang.String getColumnName(int col)
           
 GenericTableColumn<T,?> getGenericColumn(int col)
          Returns the GenericTableColumn associated with the given column index.
 int getRowCount()
           
 T getRowValue(int row)
          Returns the value that corresponds to the given row of the table
 java.lang.Object getValueAt(int row, int col)
           
 boolean isCellEditable(int row, int col)
           
 boolean isNameUsedInHeader()
          Returns true if property names are to be used in the headers of the corresponding columns.
 void setNameUsedInHeader(boolean b)
          Enables/Disables use of property names in the column headers of all columns subsequently added to the table.
 void setValueAt(java.lang.Object value, int row, int col)
           
 
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

GenericTableModel

public GenericTableModel(java.lang.Class<T> rowtype,
                         java.util.List<T> values)
Builds a GenericTableModel for the given list of data. The columns must be added separately before the table may be rendered.


GenericTableModel

public GenericTableModel(java.lang.Class<T> rowtype,
                         java.util.List<T> values,
                         java.util.List<GenericTableColumn<T,?>> columns)
Builds a GenericTableModel for the given list of data, using the given list of columns.

Method Detail

getColumnModel

public javax.swing.table.TableColumnModel getColumnModel()
Returns the column model that should be used in conjunction with this data model.


getRowValue

public T getRowValue(int row)
Returns the value that corresponds to the given row of the table


setNameUsedInHeader

public void setNameUsedInHeader(boolean b)
Enables/Disables use of property names in the column headers of all columns subsequently added to the table. Existing header values will not be changed when this value changes. Defaults to true (enabled).


isNameUsedInHeader

public boolean isNameUsedInHeader()
Returns true if property names are to be used in the headers of the corresponding columns.


addColumn

public <C> GenericTableColumn<T,C> addColumn(java.lang.Class<C> coltype,
                                             java.lang.String name)
Adds a read-only column that uses a GetProperty functor for the named property. Do not call this method after the model has been given to a table unless the table's autoCreateColumnsFromModel flag is unset.


addColumn

public <C> GenericTableColumn<T,C> addColumn(java.lang.Class<C> coltype,
                                             java.lang.String name,
                                             boolean editable)
Adds a possibly editable column that uses a GetProperty functor for the named property, and a SetProperty functor if the column is editable. Do not call this method after the model has been given to a table unless the table's autoCreateColumnsFromModel flag is unset.


addColumn

public <C> GenericTableColumn<T,C> addColumn(java.lang.Class<C> coltype,
                                             java.lang.String name,
                                             UnaryFunctor<C,java.lang.String> formatter)
Adds a read-only column that uses a GetProperty functor for the named property and renders the property value in the given formatter. Do not call this method after the model has been given to a table unless the table's autoCreateColumnsFromModel flag is unset.


addColumn

public <C> GenericTableColumn<T,C> addColumn(java.lang.Class<C> coltype,
                                             java.lang.String name,
                                             UnaryFunctor<C,java.lang.String> formatter,
                                             UnaryFunctor<java.lang.String,C> parser)
Adds an editable column that uses a GetProperty functor for the named property, and a SetProperty functor if the column is editable, and is rendered (and edited) using the given format. Do not call this method after the model has been given to a table unless the table's autoCreateColumnsFromModel flag is unset.


addColumn

public void addColumn(GenericTableColumn<T,?> col)
Adds the given column to the table. Do not call this method after the model has been given to a table unless the table's autoCreateColumnsFromModel flag is unset.


getGenericColumn

public GenericTableColumn<T,?> getGenericColumn(int col)
Returns the GenericTableColumn associated with the given column index.


getRowCount

public int getRowCount()

getColumnCount

public int getColumnCount()

getColumnClass

public java.lang.Class<?> getColumnClass(int col)
Specified by:
getColumnClass in interface javax.swing.table.TableModel
Overrides:
getColumnClass in class javax.swing.table.AbstractTableModel

getColumnName

public java.lang.String getColumnName(int col)
Specified by:
getColumnName in interface javax.swing.table.TableModel
Overrides:
getColumnName in class javax.swing.table.AbstractTableModel

isCellEditable

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

getValueAt

public java.lang.Object getValueAt(int row,
                                   int col)

setValueAt

public void setValueAt(java.lang.Object value,
                       int row,
                       int col)
Specified by:
setValueAt in interface javax.swing.table.TableModel
Overrides:
setValueAt in class javax.swing.table.AbstractTableModel


Copyright © 2002-2006 David A. Hall. All Rights Reserved.