JIDE 3.5.15

com.jidesoft.grid
Class BasicTableModel<T>

java.lang.Object
  extended by javax.swing.table.AbstractTableModel
      extended by com.jidesoft.grid.BasicTableModel<T>
All Implemented Interfaces:
ContextSensitiveTableModel, PropertyChangeListener, Serializable, EventListener, TableModel
Direct Known Subclasses:
BeanTableModel, HibernateTableModel

public class BasicTableModel<T>
extends AbstractTableModel
implements ContextSensitiveTableModel, PropertyChangeListener

BasicTableModel provides a quick way to create a table model from a list of objects. It leverages Introspector to introspect the object and display the value of the properties as each column.

Assuming you have a list of MyBeanObject which follows the JavaBean pattern, you can create a table model that allow your user to edit the property of the objects using

 BasicTableModel tableModel = new BasicTableModel(objects, MyBeanObject.class);
 
You can also ask Introspector to create such a BasicTableModel using
 new BasicTableModel(objects, IntrospectorManager.getIntrospector(MyBeanObject.class));
 
However to make the code above working, you need to register an Introspector first using code like below. Please note, the string array allows you to decide what properties to be used in the BasicTableModel.
 IntrospectorManager.registerIntrospector(MyBeanObject.class, new IntrospectorFactory() {
     public Introspector create() {
         try {
             return new BeanIntrospector(MyBeanObject.class, new String[]{"name", "Name", "text", "Text", "icon",
 "Icon", "opaque", "Opaque", "toolTipText", "ToolTip", "background", "Background", "foreground", "Foreground"}, 2);
         }
         catch (IntrospectionException e) {
             return null;
         }
     }
 });
 
In this example, since Introspector is an interface, we used the BeanIntrospector which will introspect the object using Java bean pattern. If you have other ways to introspect the object, you can implement Introspector to do it.

See Also:
Serialized Form

Field Summary
static String PROPERTY_EDITABLE
           
 
Fields inherited from class javax.swing.table.AbstractTableModel
listenerList
 
Constructor Summary
BasicTableModel()
          Creates an empty BasicTableModel.
BasicTableModel(List<T> objects, Class<?> type)
          Creates a BasicTableModel from a list of objects using the default BeanIntrospector for the specified type.
BasicTableModel(List<T> objects, Class<?> type, IntrospectorContext context)
          Creates a BasicTableModel from a list of objects using the default BeanIntrospector for the specified type and the specified BeanIntrospectorContext.
BasicTableModel(List<T> objects, Class<?> type, String[] propertyNames)
          Creates a BasicTableModel from a list of objects using the specified property names as the value for each column of the table model.
BasicTableModel(List<T> objects, Introspector introspector)
          Creates a BasicTableModel from a list of objects using the specified Introspector.
 
Method Summary
 void addObject(int index, T object)
          Adds an object to the specified index of the object list.
 void addObject(T object)
          Adds an object to the end of the object list.
 void addObjects(List<T> objects)
          Adds objects to the table model.
 void addPropertyChangeListener(PropertyChangeListener listener)
          Adds a PropertyChangeListener to the listener list.
 void bind(List<T> objects)
          If the objects follow the JavaBean pattern, it will support property change event.
 void clear()
          Removes all objects from the table model.
protected  void firePropertyChange(String propertyName, Object oldValue, Object newValue)
          Support for reporting property changes for boolean properties.
 void fireTableStructureChanged()
           
 Class<?> getCellClassAt(int row, int column)
          Gets the type at cell (row, column).
 Class<?> getColumnClass(int columnIndex)
           
 int getColumnCount()
          Gets the column count.
 String getColumnName(int columnIndex)
           
 ConverterContext getConverterContextAt(int rowIndex, int columnIndex)
          Gets the converter context at cell (row, column).
 EditorContext getEditorContextAt(int rowIndex, int columnIndex)
          Gets the editor context at cell (row, column).
 T getObject(int rowIndex)
          Gets the object from the list of the objects.
 Property getPropertyAt(int columnIndex)
          Gets the Property at the column index.
 int getRowCount()
          Gets the row count.
 Object getValueAt(int rowIndex, int columnIndex)
           
protected  void installListener(T t)
           
 boolean isCellEditable(int rowIndex, int columnIndex)
           
 boolean isEditable()
          Get the flag indicating if the entire table model is editable.
protected  void prepareProperty(Property property, int rowIndex, int columnIndex)
          This method is called in getValueAt and setValueAt method.
 void propertyChange(PropertyChangeEvent evt)
          Updates the corresponding cell if the object's property changed.
 void removeObject(int index)
          Removes an object from the object list at the specified index.
 void removeObject(T object)
          Removes an object from the object list.
 void removePropertyChangeListener(PropertyChangeListener listener)
          Removes a PropertyChangeListener from the listener list.
 void setEditable(boolean editable)
          Set the flag indicating if the entire table model is editable.
 void setObject(T object, int rowIndex)
          Sets the object at the specified index.
 void setValueAt(Object value, int rowIndex, int columnIndex)
           
 void unbind(List<T> objects)
          Uninstalled the listeners that were installed in the bind method.
protected  void uninstallListener(T t)
           
 
Methods inherited from class javax.swing.table.AbstractTableModel
addTableModelListener, findColumn, fireTableCellUpdated, fireTableChanged, fireTableDataChanged, fireTableRowsDeleted, fireTableRowsInserted, fireTableRowsUpdated, getListeners, getTableModelListeners, removeTableModelListener
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface javax.swing.table.TableModel
addTableModelListener, removeTableModelListener
 

Field Detail

PROPERTY_EDITABLE

public static final String PROPERTY_EDITABLE
See Also:
Constant Field Values
Constructor Detail

BasicTableModel

public BasicTableModel()
Creates an empty BasicTableModel.


BasicTableModel

public BasicTableModel(List<T> objects,
                       Class<?> type)
Creates a BasicTableModel from a list of objects using the default BeanIntrospector for the specified type. You need to register the BeanIntrospector on BeanIntrospectorManager first.

Parameters:
objects - the list of objects.
type - the object type

BasicTableModel

public BasicTableModel(List<T> objects,
                       Class<?> type,
                       IntrospectorContext context)
Creates a BasicTableModel from a list of objects using the default BeanIntrospector for the specified type and the specified BeanIntrospectorContext. You need to register the BeanIntrospector on BeanIntrospectorManager first.

Parameters:
objects - the list of objects.
type - the object type
context - the IntrospectorContext. We will use type and context to locate the Introspector that was registered in IntrospectorManager.

BasicTableModel

public BasicTableModel(List<T> objects,
                       Class<?> type,
                       String[] propertyNames)
                throws IntrospectionException
Creates a BasicTableModel from a list of objects using the specified property names as the value for each column of the table model. You can also use BasicTableModel(java.util.List, Class, com.jidesoft.introspector.IntrospectorContext) so that you can assign both name and display names using a single array.

Parameters:
objects - the list of objects.
type - the object type
propertyNames - an array of property names. The array is in the format of { "propertyName1", "displayName11", "propertyName2", "displayName12", ... };
Throws:
IntrospectionException - if java bean introspection failed.

BasicTableModel

public BasicTableModel(List<T> objects,
                       Introspector introspector)
Creates a BasicTableModel from a list of objects using the specified Introspector.

Parameters:
objects - the list of objects.
introspector - the introspector for the object
Method Detail

getRowCount

public int getRowCount()
Gets the row count. It is the length of the list of the objects.

Specified by:
getRowCount in interface TableModel
Returns:
the row count.

getObject

public T getObject(int rowIndex)
Gets the object from the list of the objects.

Parameters:
rowIndex - the row index.
Returns:
the object at the specified index.

setObject

public void setObject(T object,
                      int rowIndex)
Sets the object at the specified index. A tableRowsUpdated event will be fired.

Parameters:
object - the object
rowIndex - the row index

addObject

public void addObject(T object)
Adds an object to the end of the object list. A tableRowsInserted event will be fired.

Parameters:
object - the object to be added.

addObject

public void addObject(int index,
                      T object)
Adds an object to the specified index of the object list. A tableRowsInserted event will be fired.

Parameters:
index - the index to be inserted at.
object - the object to be added.

removeObject

public void removeObject(T object)
Removes an object from the object list. A tableRowsDeleted event will be fired if the object is indeed removed.

Parameters:
object - the object to be removed.

removeObject

public void removeObject(int index)
Removes an object from the object list at the specified index. A tableRowsDeleted event will be fired.

Parameters:
index - the index of the object to be removed.

addObjects

public void addObjects(List<T> objects)
Adds objects to the table model. They will append at the end.

Parameters:
objects - the objects to be added.

clear

public void clear()
Removes all objects from the table model.


getColumnCount

public int getColumnCount()
Gets the column count. It is the number of properties of each object.

Specified by:
getColumnCount in interface TableModel
Returns:
the column count.

getPropertyAt

public Property getPropertyAt(int columnIndex)
Gets the Property at the column index. This property doesn't bind to the row yet. If you want to get the value or set value to a particular row, you still need to call this first before calling property.setValue or property.getValue.
 property.setInstance(getObject(rowIndex));
 

Parameters:
columnIndex - the column index.
Returns:
the property.

getValueAt

public Object getValueAt(int rowIndex,
                         int columnIndex)
Specified by:
getValueAt in interface TableModel

setValueAt

public void setValueAt(Object value,
                       int rowIndex,
                       int columnIndex)
Specified by:
setValueAt in interface TableModel
Overrides:
setValueAt in class AbstractTableModel

prepareProperty

protected void prepareProperty(Property property,
                               int rowIndex,
                               int columnIndex)
This method is called in getValueAt and setValueAt method. In the case of BeanProperty, since we share the Property, we need to call setInstance method to set the actual value before we call getValueAt and setValueAt. Subclass can override it to do some customized initialization for the property.

Parameters:
property - the property for the cell.
rowIndex - the row index of the cell where the Property is used.
columnIndex - the column index of the cell where the Property is used.

getColumnName

public String getColumnName(int columnIndex)
Specified by:
getColumnName in interface TableModel
Overrides:
getColumnName in class AbstractTableModel

getColumnClass

public Class<?> getColumnClass(int columnIndex)
Specified by:
getColumnClass in interface TableModel
Overrides:
getColumnClass in class AbstractTableModel

isCellEditable

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

getConverterContextAt

public ConverterContext getConverterContextAt(int rowIndex,
                                              int columnIndex)
Description copied from interface: ContextSensitiveTableModel
Gets the converter context at cell (row, column).

For a special row index like -1, please return the default converter context for the entire column if there is any.

Specified by:
getConverterContextAt in interface ContextSensitiveTableModel
Parameters:
rowIndex - the row index
columnIndex - the column index
Returns:
converter context

getEditorContextAt

public EditorContext getEditorContextAt(int rowIndex,
                                        int columnIndex)
Description copied from interface: ContextSensitiveTableModel
Gets the editor context at cell (row, column).

For a special row index like -1, please return the default editor context for the entire column if there is any.

Specified by:
getEditorContextAt in interface ContextSensitiveTableModel
Parameters:
rowIndex - the row index
columnIndex - the column index
Returns:
editor context

getCellClassAt

public Class<?> getCellClassAt(int row,
                               int column)
Description copied from interface: ContextSensitiveTableModel
Gets the type at cell (row, column).

For a special row index like -1, please return the default column class for the entire column if there is any.

Specified by:
getCellClassAt in interface ContextSensitiveTableModel
Parameters:
row - the row index
column - the column index
Returns:
type

bind

public void bind(List<T> objects)
          throws Exception
If the objects follow the JavaBean pattern, it will support property change event. This method will add BasicTableModel as a property change listener (BasicTableModel implements PropertyChangeListener) to the object so that it will get notification when the property of those objects is changed from outside.

Parameters:
objects - the list of objects.
Throws:
Exception - it fails to bind the objects.

unbind

public void unbind(List<T> objects)
            throws Exception
Uninstalled the listeners that were installed in the bind method.

Parameters:
objects - the list of objects.
Throws:
Exception - if it fails to unbind the objects.

installListener

protected void installListener(T t)
                        throws Exception
Throws:
Exception

uninstallListener

protected void uninstallListener(T t)
                          throws Exception
Throws:
Exception

propertyChange

public void propertyChange(PropertyChangeEvent evt)
Updates the corresponding cell if the object's property changed.

Specified by:
propertyChange in interface PropertyChangeListener
Parameters:
evt - the PropertyChangeEvent

fireTableStructureChanged

public void fireTableStructureChanged()
Overrides:
fireTableStructureChanged in class AbstractTableModel

isEditable

public boolean isEditable()
Get the flag indicating if the entire table model is editable.

By default, the value is true. If you want to improve the performance by not registering unnecessary listeners while you have this table model with read only purpose, please set it to false.

Returns:
true if the table model is editable. Otherwise false.

setEditable

public void setEditable(boolean editable)
Set the flag indicating if the entire table model is editable.

Parameters:
editable - the flag
See Also:
isEditable()

firePropertyChange

protected void firePropertyChange(String propertyName,
                                  Object oldValue,
                                  Object newValue)
Support for reporting property changes for boolean properties. This method can be called when a property has changed and it will send the appropriate PropertyChangeEvent to any registered PropertyChangeListeners.

Parameters:
propertyName - the property whose value has changed
oldValue - the property's previous value
newValue - the property's new value

addPropertyChangeListener

public void addPropertyChangeListener(PropertyChangeListener listener)
Adds a PropertyChangeListener to the listener list.

If listener is null, no exception is thrown and no action is performed.

Parameters:
listener - the property change listener to be added
See Also:
removePropertyChangeListener(java.beans.PropertyChangeListener)

removePropertyChangeListener

public void removePropertyChangeListener(PropertyChangeListener listener)
Removes a PropertyChangeListener from the listener list. This method should be used to remove PropertyChangeListeners that were registered for all bound properties of this class.

If listener is null, no exception is thrown and no action is performed.

Parameters:
listener - the PropertyChangeListener to be removed
See Also:
addPropertyChangeListener(java.beans.PropertyChangeListener)

JIDE 3.5.15