Package com.gargoylesoftware.base.gui
Class ReflectedTableModel
- java.lang.Object
-
- javax.swing.table.AbstractTableModel
-
- com.gargoylesoftware.base.gui.ReflectedTableModel
-
- All Implemented Interfaces:
java.io.Serializable,javax.swing.table.TableModel
public class ReflectedTableModel extends javax.swing.table.AbstractTableModelA table model that uses reflection to retrieve values out of the row objects.The sample below will create a JTable with one row of data and one column per property in the Date class (Date has 10 properties in JDK1.3).
final JTable table = new JTable(); final ReflectedTableModel model = new ReflectedTableModel(Date.class); model.getRows().add( new Date() ); table.setModel(model);
This sample will only provide columns for month and year.final JTable table = new JTable(); final ReflectedTableModel model = new ReflectedTableModel(); model.getRows().add( new Date() ); model.getColumns().add( new ReflectedTableModel.ColumnInfo("month") ); model.getColumns().add( new ReflectedTableModel.ColumnInfo("year") ); table.setModel(model);Tip: To enable debugging information callsetTraceChannel(TraceChannel)with a non-null TraceChannel.model.setTraceChannel(Trace.out)
- Version:
- $Revision: 1.7 $
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classReflectedTableModel.ColumnInfoThis class contains information about one specific column in the table.private classReflectedTableModel.RowElementControlData
-
Field Summary
Fields Modifier and Type Field Description private NotificationListListenercolumnListener_If any change occurs to the columns then fire a structure changed.private java.util.Listcolumns_private static java.lang.Object[]EMPTY_OBJECT_ARRAYprivate java.beans.PropertyChangeListenerpropertyChangeListener_private java.util.MaprowElementControlDatas_private NotificationListListenerrowListener_If the row list changes then fire the appropriate table event.private java.util.Listrows_private static longserialVersionUIDprivate TraceChanneltraceChannel_
-
Constructor Summary
Constructors Constructor Description ReflectedTableModel()Create an empty model with no columns and no rows.ReflectedTableModel(java.lang.Class clazz)Create an empty model with no rows but the columns preset to match the properties in the given class.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private voidaddRowElement(java.lang.Object object)Add a row elementprivate voidaddRowElements(java.util.List list)protected voidassertNotNull(java.lang.String fieldName, java.lang.Object object)Throw an exception if the specified object is nullintgetColumnCount()Return the number of columns.java.lang.StringgetColumnName(int index)Return the name of the column at the specified index.java.util.ListgetColumns()Return a list containing the ColumnInfo objects that are used to define each column.intgetRowCount()Return the number of rows.java.util.ListgetRows()Return a list containing the objects that are used to create each row.TraceChannelgetTraceChannel()Return the channel currently being used for tracing or null if tracing is disabled.java.lang.ObjectgetValueAt(int rowIndex, int columnIndex)Return the specified object.private voidremoveRowElement(java.lang.Object object)Remove one rowprivate voidremoveRowElements(java.util.List list)voidsetTraceChannel(TraceChannel channel)Set the channel to be used for tracing.-
Methods inherited from class javax.swing.table.AbstractTableModel
addTableModelListener, findColumn, fireTableCellUpdated, fireTableChanged, fireTableDataChanged, fireTableRowsDeleted, fireTableRowsInserted, fireTableRowsUpdated, fireTableStructureChanged, getColumnClass, getListeners, getTableModelListeners, isCellEditable, removeTableModelListener, setValueAt
-
-
-
-
Field Detail
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
rowListener_
private NotificationListListener rowListener_
If the row list changes then fire the appropriate table event.
-
columnListener_
private NotificationListListener columnListener_
If any change occurs to the columns then fire a structure changed.
-
propertyChangeListener_
private java.beans.PropertyChangeListener propertyChangeListener_
-
rowElementControlDatas_
private final java.util.Map rowElementControlDatas_
-
rows_
private final java.util.List rows_
-
columns_
private final java.util.List columns_
-
EMPTY_OBJECT_ARRAY
private static final java.lang.Object[] EMPTY_OBJECT_ARRAY
-
traceChannel_
private TraceChannel traceChannel_
-
-
Constructor Detail
-
ReflectedTableModel
public ReflectedTableModel()
Create an empty model with no columns and no rows.
-
ReflectedTableModel
public ReflectedTableModel(java.lang.Class clazz) throws java.beans.IntrospectionExceptionCreate an empty model with no rows but the columns preset to match the properties in the given class.- Parameters:
clazz- The class to get properties from.- Throws:
java.beans.IntrospectionException- If the Introspector is unable to get the properties for this class.
-
-
Method Detail
-
getRows
public java.util.List getRows()
Return a list containing the objects that are used to create each row. This list is backed by the original store such that changes to this list will be reflected in the table model.- Returns:
- The rows.
-
getColumns
public java.util.List getColumns()
Return a list containing the ColumnInfo objects that are used to define each column. This list is backed by the original store such that changes to this list will be reflected in the table model.- Returns:
- The columns.
-
getColumnCount
public int getColumnCount()
Return the number of columns.- Returns:
- the number of columns.
-
getRowCount
public int getRowCount()
Return the number of rows.- Returns:
- The number of rows.
-
getValueAt
public java.lang.Object getValueAt(int rowIndex, int columnIndex)Return the specified object.- Parameters:
rowIndex- The row indexcolumnIndex- The columnIndex- Returns:
- The object at the specified row and column.
-
getColumnName
public java.lang.String getColumnName(int index)
Return the name of the column at the specified index.- Specified by:
getColumnNamein interfacejavax.swing.table.TableModel- Overrides:
getColumnNamein classjavax.swing.table.AbstractTableModel- Parameters:
index- The index of the column.- Returns:
- The name of the column at the specified index.
-
setTraceChannel
public void setTraceChannel(TraceChannel channel)
Set the channel to be used for tracing.- Parameters:
channel- The channel to be used for tracing or null if tracing is to be disabled.
-
getTraceChannel
public TraceChannel getTraceChannel()
Return the channel currently being used for tracing or null if tracing is disabled.- Returns:
- The trace channel or null if a channel hasn't been set.
-
addRowElement
private void addRowElement(java.lang.Object object)
Add a row element- Parameters:
object- the object that will be used to populate this row
-
addRowElements
private void addRowElements(java.util.List list)
- Parameters:
list- The list of objects that will be used to create the rows.
-
removeRowElements
private void removeRowElements(java.util.List list)
- Parameters:
list- The list of object that will be removed from the model
-
removeRowElement
private void removeRowElement(java.lang.Object object)
Remove one row- Parameters:
object- The object that was used to create this row.
-
assertNotNull
protected final void assertNotNull(java.lang.String fieldName, java.lang.Object object)Throw an exception if the specified object is null- Parameters:
fieldName- The name of the paremeter we are checkingobject- The value of the parameter we are checking
-
-