| java.lang.Object javax.swing.table.AbstractTableModel net.sf.jga.swing.GenericTableModel
GenericTableModel | public class GenericTableModel extends AbstractTableModel (Code) | | 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 |
Method Summary | |
public GenericTableColumn<T, C> | addColumn(Class<C> coltype, String name) Adds a read-only column that uses a GetProperty functor for the named
property. | public GenericTableColumn<T, C> | addColumn(Class<C> coltype, 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. | public GenericTableColumn<T, C> | addColumn(Class<C> coltype, String name, UnaryFunctor<C, 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. | public GenericTableColumn<T, C> | addColumn(Class<C> coltype, String name, UnaryFunctor<C, String> formatter, UnaryFunctor<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. | public void | addColumn(GenericTableColumn<T, ?> col) Adds the given column to the table. | public Class | getColumnClass(int col) | public int | getColumnCount() | public TableColumnModel | getColumnModel() Returns the column model that should be used in conjunction with this
data model. | public String | getColumnName(int col) | public GenericTableColumn<T, ?> | getGenericColumn(int col) Returns the GenericTableColumn associated with the given column index. | public int | getRowCount() | public T | getRowValue(int row) | public Object | getValueAt(int row, int col) | public boolean | isCellEditable(int row, int col) | public boolean | isNameUsedInHeader() Returns true if property names are to be used in the headers of the
corresponding columns. | public void | setNameUsedInHeader(boolean b) Enables/Disables use of property names in the column headers of all
columns subsequently added to the table. | public void | setValueAt(Object value, int row, int col) |
serialVersionUID | final static long serialVersionUID(Code) | | |
GenericTableModel | public GenericTableModel(Class<T> rowtype, List<T> values)(Code) | | Builds a GenericTableModel for the given list of data. The columns
must be added separately before the table may be rendered.
|
GenericTableModel | public GenericTableModel(Class<T> rowtype, List<T> values, List<GenericTableColumn<T, ?>> columns)(Code) | | Builds a GenericTableModel for the given list of data, using the
given list of columns.
|
addColumn | public GenericTableColumn<T, C> addColumn(Class<C> coltype, String name)(Code) | | 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 GenericTableColumn<T, C> addColumn(Class<C> coltype, String name, boolean editable)(Code) | | 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 GenericTableColumn<T, C> addColumn(Class<C> coltype, String name, UnaryFunctor<C, String> formatter)(Code) | | 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 GenericTableColumn<T, C> addColumn(Class<C> coltype, String name, UnaryFunctor<C, String> formatter, UnaryFunctor<String, C> parser)(Code) | | 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)(Code) | | 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.
|
getColumnClass | public Class getColumnClass(int col)(Code) | | |
getColumnCount | public int getColumnCount()(Code) | | |
getColumnModel | public TableColumnModel getColumnModel()(Code) | | Returns the column model that should be used in conjunction with this
data model.
|
getGenericColumn | public GenericTableColumn<T, ?> getGenericColumn(int col)(Code) | | Returns the GenericTableColumn associated with the given column index.
|
getRowCount | public int getRowCount()(Code) | | |
getRowValue | public T getRowValue(int row)(Code) | | Returns the value that corresponds to the given row of the table
|
isCellEditable | public boolean isCellEditable(int row, int col)(Code) | | |
isNameUsedInHeader | public boolean isNameUsedInHeader()(Code) | | Returns true if property names are to be used in the headers of the
corresponding columns.
|
setNameUsedInHeader | public void setNameUsedInHeader(boolean b)(Code) | | 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).
|
setValueAt | public void setValueAt(Object value, int row, int col)(Code) | | |
|
|