|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object javax.swing.table.AbstractTableModel net.sf.jga.swing.GenericTableModel<T>
public class GenericTableModel<T>
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
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 | ||
---|---|---|
|
addColumn(java.lang.Class<C> coltype,
java.lang.String name)
Adds a read-only column that uses a GetProperty functor for the named property. |
|
|
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. |
|
|
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. |
|
|
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 |
---|
public GenericTableModel(java.lang.Class<T> rowtype, java.util.List<T> values)
public GenericTableModel(java.lang.Class<T> rowtype, java.util.List<T> values, java.util.List<GenericTableColumn<T,?>> columns)
Method Detail |
---|
public javax.swing.table.TableColumnModel getColumnModel()
public T getRowValue(int row)
public void setNameUsedInHeader(boolean b)
public boolean isNameUsedInHeader()
public <C> GenericTableColumn<T,C> addColumn(java.lang.Class<C> coltype, java.lang.String name)
public <C> GenericTableColumn<T,C> addColumn(java.lang.Class<C> coltype, java.lang.String name, boolean editable)
public <C> GenericTableColumn<T,C> addColumn(java.lang.Class<C> coltype, java.lang.String name, UnaryFunctor<C,java.lang.String> formatter)
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)
public void addColumn(GenericTableColumn<T,?> col)
public GenericTableColumn<T,?> getGenericColumn(int col)
public int getRowCount()
public int getColumnCount()
public java.lang.Class<?> getColumnClass(int col)
getColumnClass
in interface javax.swing.table.TableModel
getColumnClass
in class javax.swing.table.AbstractTableModel
public java.lang.String getColumnName(int col)
getColumnName
in interface javax.swing.table.TableModel
getColumnName
in class javax.swing.table.AbstractTableModel
public boolean isCellEditable(int row, int col)
isCellEditable
in interface javax.swing.table.TableModel
isCellEditable
in class javax.swing.table.AbstractTableModel
public java.lang.Object getValueAt(int row, int col)
public void setValueAt(java.lang.Object value, int row, int col)
setValueAt
in interface javax.swing.table.TableModel
setValueAt
in class javax.swing.table.AbstractTableModel
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |