com.sciapp.table
Class ObjectTableModel

java.lang.Object
  extended by javax.swing.table.AbstractTableModel
      extended by com.sciapp.table.ObjectTableModel
All Implemented Interfaces:
ListTableModel, Serializable, TableModel

public abstract class ObjectTableModel
extends AbstractTableModel
implements ListTableModel

ObjectTableModel uses an internal ArrayList structure to store the rows of a JTable. The elements in the list can be any Java object. Subclasses should implement getValueAt(int, int) and setValueAt(java.lang.Object, int, int), which define how objects at a column index are retrieved/set respectively. The column names are specified in the constructor, or with setColumns(java.lang.String[])


Field Summary
protected  String[] columns
          A String array holding the columns of the TableModel
protected  ArrayList rows
          The actual table rows stored in an ArrayList.
 
Fields inherited from class javax.swing.table.AbstractTableModel
listenerList
 
Constructor Summary
ObjectTableModel(String[] columns)
          Constructs an ObjectTableModel with columns as the table columns.
 
Method Summary
 void addRow(Object o)
          Adds a row to the end of the model.
 void addRows(List addedRows)
          Adds a list of rows to the end of the model.
 void clear()
          Clears the model of any data.
 Object getCellValue(Object o, int index)
          Returns the Object found at index of row passed as a parameter.
 Class getColumnClass(int c)
          Returns the class of the object at column c.
 int getColumnCount()
          Returns the number of columns in the model.
 String getColumnName(int column)
          Returns the name of the column at column, which depends on the String array passed in the constructor.
 String[] getColumns()
          Returns a clone of the String array of table column names.
 int getRowCount()
          Returns the number of rows in the model.
 List getRows()
          Returns the data of this tablemodel
 Object getValueAt(int rowIndex, int columnIndex)
          Returns the value for the cell at columnIndex and rowIndex.
abstract  Object getValueAt(Object o, int index)
          Returns the value for the object o at index.
 boolean isCellEditable(int rowIndex, int columnIndex)
          Returns true by default.
 void removeRow(int row)
          Removes a row from the data model
 void removeRows(int[] deletedRows)
          Removes a few rows from the data model
 void setColumns(String[] columns)
          Sets the table columns.
 void setValueAt(Object aValue, int rowIndex, int columnIndex)
          Assigns aValue to the object at columnIndex and rowIndex.
abstract  void setValueAt(Object o, Object aValue, int index)
          Sets the value of Object o at index to aValue
 
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
 
Methods inherited from interface com.sciapp.table.ListTableModel
fireTableCellUpdated, fireTableChanged, fireTableDataChanged, fireTableRowsDeleted, fireTableRowsInserted, fireTableRowsUpdated, fireTableStructureChanged
 
Methods inherited from interface javax.swing.table.TableModel
addTableModelListener, removeTableModelListener
 

Field Detail

rows

protected ArrayList rows
The actual table rows stored in an ArrayList.


columns

protected String[] columns
A String array holding the columns of the TableModel

Constructor Detail

ObjectTableModel

public ObjectTableModel(String[] columns)
Constructs an ObjectTableModel with columns as the table columns.

Method Detail

addRow

public void addRow(Object o)
Adds a row to the end of the model.

Specified by:
addRow in interface ListTableModel
Parameters:
o - the row being added

addRows

public void addRows(List addedRows)
Adds a list of rows to the end of the model.

addedRows contains objects representing the actual rows being added. e.g. java.util.Vector, for DefaultTableModel.

Specified by:
addRows in interface ListTableModel
Parameters:
addedRows - the rows being added.

clear

public void clear()
Clears the model of any data.

Specified by:
clear in interface ListTableModel

getCellValue

public Object getCellValue(Object o,
                           int index)
Returns the Object found at index of row passed as a parameter.

Specified by:
getCellValue in interface ListTableModel
Parameters:
o - the row data
index - an index
Returns:
the object found at column index of the row data row

getColumnClass

public Class getColumnClass(int c)
Returns the class of the object at column c. This method first fetches the object at row 0 and column c and then returns getClass() on that object.
If the object is null, Object.class is returned. Do not call this method, if there are no data in the tablemodel, since an OutOfIndexException will be thrown.

Specified by:
getColumnClass in interface TableModel
Overrides:
getColumnClass in class AbstractTableModel
Parameters:
c - the column
Returns:
the class of the object at columnc.

getColumnCount

public int getColumnCount()
Returns the number of columns in the model. A JTable uses this method to determine how many columns it should create and display by default.

Specified by:
getColumnCount in interface TableModel
Returns:
the number of columns in the model
See Also:
TableModel.getRowCount()

getColumnName

public String getColumnName(int column)
Returns the name of the column at column, which depends on the String array passed in the constructor.

Specified by:
getColumnName in interface TableModel
Overrides:
getColumnName in class AbstractTableModel
Parameters:
column - the column being queried
Returns:
the name of the column at column.

getColumns

public String[] getColumns()
Returns a clone of the String array of table column names.

Returns:
the table columns

getRowCount

public int getRowCount()
Returns the number of rows in the model. A JTable uses this method to determine how many rows it should display. This method should be quick, as it is called frequently during rendering.

Specified by:
getRowCount in interface TableModel
Returns:
the number of rows in the model
See Also:
TableModel.getColumnCount()

getRows

public List getRows()
Returns the data of this tablemodel

Specified by:
getRows in interface ListTableModel
Returns:
the rows of the datamodel as a java.util.List

getValueAt

public Object getValueAt(int rowIndex,
                         int columnIndex)
Returns the value for the cell at columnIndex and rowIndex.

Specified by:
getValueAt in interface TableModel
Parameters:
rowIndex - the row whose value is to be queried
columnIndex - the column whose value is to be queried
Returns:
the value Object at the specified cell

getValueAt

public abstract Object getValueAt(Object o,
                                  int index)
Returns the value for the object o at index.

Parameters:
o - the object whose value is to be queried
index - the column whose value is to be queried
Returns:
the value of the Object at the specified index

isCellEditable

public boolean isCellEditable(int rowIndex,
                              int columnIndex)
Returns true by default.

Specified by:
isCellEditable in interface TableModel
Overrides:
isCellEditable in class AbstractTableModel
Parameters:
rowIndex - the row being queried
columnIndex - the column being queried
Returns:
true
See Also:
TableModel.setValueAt(java.lang.Object, int, int)

removeRow

public void removeRow(int row)
Removes a row from the data model

Specified by:
removeRow in interface ListTableModel
Parameters:
row - the index of the row being removed

removeRows

public void removeRows(int[] deletedRows)
Removes a few rows from the data model

Specified by:
removeRows in interface ListTableModel
Parameters:
deletedRows - an integer array that contains indexes of the rows being deleted

setColumns

public void setColumns(String[] columns)
Sets the table columns.

Parameters:
columns - the table columns as an array of Strings

setValueAt

public void setValueAt(Object aValue,
                       int rowIndex,
                       int columnIndex)
Assigns aValue to the object at columnIndex and rowIndex.

Specified by:
setValueAt in interface TableModel
Overrides:
setValueAt in class AbstractTableModel
Parameters:
aValue - value to assign to the cell
rowIndex - row of the cell
columnIndex - column of the cell
See Also:
TableModel.getValueAt(int, int), TableModel.isCellEditable(int, int)

setValueAt

public abstract void setValueAt(Object o,
                                Object aValue,
                                int index)
Sets the value of Object o at index to aValue

Parameters:
o - the object whose value is to be changed
aValue - the new value
index - the index whose value is to be changed