All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class com.sun.java.swing.table.SimpleTableModel

java.lang.Object
   |
   +----com.sun.java.swing.table.AbstractTableModel
           |
           +----com.sun.java.swing.table.SimpleTableModel

public class SimpleTableModel
extends AbstractTableModel
implements Serializable
This is an implementation of TableModel that uses a Vector of Vectors to store the cell value objects. The benefits of this implementation is that it is very simple to use, specially when it is coupled with JSimpleTable. Users is relieve of the task of writing a TableModel, instead users can think only in terms of a grid of values. In addition, because the model knows all the values it can provide sorting support. (PENDING(alan) - support for sorting needs to be added) Note: The SimpleTableModel's API contains the methods addColumn(), removeColumn(), but not methods to insert a column at an index nor methods to move the columns. This is because JTable does not display the columns based on the order of the columns in this model. So rearranging them here doesn't do much. See the column ordering methods in JTable. Or use JSimpleTable for a simpler API of JTable.

See Also:
getDataVector

Variable Index

 o columnIdentifiers
The Vector column identifiers
 o dataVector
The Vector of Vector of Object values

Constructor Index

 o SimpleTableModel()
Constructs a default SimpleTableModel which is a table of zero columns and zero rows.
 o SimpleTableModel(int, int)
Constructs a SimpleTableModel with numColumns and numRows of null object values.
 o SimpleTableModel(Vector, int)
Constructs a SimpleTableModel with as many columns as there are elements in columnIds and numRows of null object values.
 o SimpleTableModel(Vector, Vector)
Constructs a SimpleTableModel and initialize the table by passing data and columnIds to the setDataVector() method.

Method Index

 o addColumn(Object, Vector)
Add a column to the model.
 o addRow(Vector)
Add a row to the end of the model.
 o generateIds(int)
 o getColumnCount()
 o getColumnIdentifiers()
Returns an enumeration of the column identifiers of this model.
 o getDataVector()
This returns the Vector of Vectors that contains the table's data values.
 o getRowCount()
 o getValueAt(Object, int)
 o identifierOfColumn(int)
Returns the unique identifier object of the column at index.
 o indexOfColumn(Object)
Returns the index of the column with an unique identifier that matches the parameter columnIdentifier, using the equals() method.
 o insertRow(int, Vector)
Insert a row at rowIndex in the model.
 o moveRow(int, int, int)
 o newDataAvailable(TableModelEvent)
This method is used to informed the model that the model's dataVector has been changed directly.
 o newRowsAdded(TableModelEvent)
This method will recalculate numRows, it will also make sure the new rows have the correct number of columns.
 o removeColumn(Object)
Remove the column with an unique identifier that matches the parameter columnIdentifier, using the equals() method.
 o removeRow(int)
Remove the row at rowIndex from the model.
 o rowsRemoved(TableModelEvent)
This method will recalculate numRows, then it will send out the tableRowsRemoved() notification message to all my listeners.
 o setColumnIdentifier(Object, Object)
Replaces the first identifier that equals oldIdentifier with newIdentifier.
 o setDataVector(Vector, Vector)
This replaces the current dataVector instance variable with the parameter newData.
 o setNumColumns(Vector)
Sets the number of columns in the model.
 o setNumRows(int)
Sets the number of rows in the model.
 o setValueAt(Object, Object, int)
Sets the object value for the cell in the column with the unique identifier ColumnIdentifier at rowIndex.

Variables

 o dataVector
 protected Vector dataVector
The Vector of Vector of Object values

 o columnIdentifiers
 protected Vector columnIdentifiers
The Vector column identifiers

Constructors

 o SimpleTableModel
 public SimpleTableModel()
Constructs a default SimpleTableModel which is a table of zero columns and zero rows.

 o SimpleTableModel
 public SimpleTableModel(int numColumns,
                         int numRows)
Constructs a SimpleTableModel with numColumns and numRows of null object values. The columns will each be given a unique identifier that is a String of form "Column0", "Column1", "Column2", etc.

Parameters:
numColumns - The number of columns the table holds
numRows - The number of rows the table holds
See Also:
setValueAt, setColumnIdentifier
 o SimpleTableModel
 public SimpleTableModel(Vector columnIds,
                         int numRows)
Constructs a SimpleTableModel with as many columns as there are elements in columnIds and numRows of null object values. Each column's unique identifier object will be taken from the columnIds vector.

Parameters:
columnIds - Vector containing unique identifier objects to be used to identify the columns. If this null then the model has no columns
numRows - The number of rows the table holds
See Also:
setValueAt, setColumnIdentifier
 o SimpleTableModel
 public SimpleTableModel(Vector data,
                         Vector columnIds)
Constructs a SimpleTableModel and initialize the table by passing data and columnIds to the setDataVector() method. See the documentation of the getDataVector() method for details on how to structure data properly.

Parameters:
data - The data of the table
Throws: IllegalArgumentException
see setDataVector().
See Also:
getDataVector, setDataVector

Methods

 o getColumnCount
 public int getColumnCount()
Returns:
the number of columns in the model.
 o getColumnIdentifiers
 public Enumeration getColumnIdentifiers()
Returns an enumeration of the column identifiers of this model. The ordering of the identifiers maps to the ordering of columns in the dataVector.

Returns:
an enumeration of the column identifiers of this model.
 o setColumnIdentifier
 public void setColumnIdentifier(Object oldIdentifier,
                                 Object newIdentifier)
Replaces the first identifier that equals oldIdentifier with newIdentifier.

Parameters:
oldIdentifier - The identifier to be replaced
oldIdentifier - The new identifier
Throws: IllegalArgumentException
if oldIdentifier can't be found, or if any of the parameters are null.
 o indexOfColumn
 public int indexOfColumn(Object columnIdentifier)
Returns the index of the column with an unique identifier that matches the parameter columnIdentifier, using the equals() method. This returns -1 if columnIdentifier is null or if no match is found.

Parameters:
columnIdentifier - the identifier of the column we are looking for
Returns:
the column index of column matching columnIdentifier, or -1.
 o identifierOfColumn
 public Object identifierOfColumn(int index)
Returns the unique identifier object of the column at index.

Returns:
The identifier object of the column at index
Throws: ArrayIndexOutOfBoundsException
if the index was invalid.
 o getDataVector
 public Vector getDataVector()
This returns the Vector of Vectors that contains the table's data values. The vectors contained in the outer vector are each a single row of values. In other words, to get to the cell at column 5, row 1:

(Vector)(getDataVector().elementAt(1)).elementAt(5);

You can directly alter the returned Vector. You can change the cell values, the number of rows, but you may not change the number of columns in the model. If you need to alter the number of columns in the model, you can do so with addColumn(), removeColumn(), or the setDataVector() methods. Once you have finished modifying the dataVector, you must inform the model of the new data using the newDataAvailable(), rowsRemoved() or the newRowsAdded() methods. These methods will give the model a chance to modify its internal variables based upon the new data vector. The new data methods will also generate the appropriate TableModelListener messages to notify any JTables using this model of the new data.

See Also:
newDataAvailable, newRowsAdded, rowsRemoved, setDataVector
 o setDataVector
 public void setDataVector(Vector newData,
                           Vector columnIds)
This replaces the current dataVector instance variable with the parameter newData. columnIds are the unique identifiers of the new columns. The first identifier in columnIds is mapped to column 1 in newData.

The size of the columnIds vector must equal the size of the newData vector. If columnIds is null then it will reuse the currect vector of column identifiers. Finally, this method calls newDataAvailable() to generate the appropriate notification.

Parameters:
newData - The new data vector
columnIds - The corresponding unique column identifiers for the new data vector
Throws: IllegalArgumentException
if newData is null or if newData.size() does not equal the number of the column identifiers.
See Also:
newDataAvailable, getDataVector
 o newDataAvailable
 public void newDataAvailable(TableModelEvent event)
This method is used to informed the model that the model's dataVector has been changed directly. The event describes the extend of the update. This method will send the tableChanged() notification message to all my listeners.

If event is null or if the number of rows in dataVector is different from numRows, then it will assume all the data in dataVector is new or updated. It will recalculate numRows, and will generate the correct TableModelEvent to send out with the nofication.

Parameters:
eter - event This TableModelEvent describes where the change happened. If null it assumes all data is new or updated
See Also:
getDataVector, newRowsAdded, rowsRemoved
 o newRowsAdded
 public void newRowsAdded(TableModelEvent event)
This method will recalculate numRows, it will also make sure the new rows have the correct number of columns. Then it will send out the tableRowsInserted() notification message to all my listeners.

Parameters:
eter - event This TableModelEvent describes where the rows were added. If null it assumes all the rows were newly added.
See Also:
getDataVector
 o rowsRemoved
 public void rowsRemoved(TableModelEvent event)
This method will recalculate numRows, then it will send out the tableRowsRemoved() notification message to all my listeners.

Parameters:
eter - event This TableModelEvent describes where the rows were removed.
Throws: IllegalArgumentException
if event is null
See Also:
getDataVector
 o setNumColumns
 public void setNumColumns(Vector newIdentifiers)
Sets the number of columns in the model. If the number of newIdentifier is greater than the current numColumns, new columns are added to the end of each row in the model. If the number of newIdentifier is less than the current number of columns, all the extra columns at the end of a row are discarded.

Parameters:
newIdentifier - Vector of column identifiers. A null means setting the model to zero columns
 o setNumRows
 public void setNumRows(int newSize)
Sets the number of rows in the model. If the new size is greater than the current size, new rows are added to the end of the model If the new size is less than the current size, all rows at index newSize and greater are discarded.

Parameters:
newSize - the new number of rows
 o addColumn
 public void addColumn(Object columnIdentifier,
                       Vector columnData)
Add a column to the model. The new column will have the unique idenitifier columnIdentifier. columnData is the optional Vector of data for the column. If it is null the column is filled with null values. Otherwise, the new data will be added to model starting with the first element going to row 0, etc.

Parameters:
columnIdentifier - the unique identifier of the column being added
columnData - optional data of the column being added
Throws: IllegalArgumentException
if columnIdentifier is null
 o removeColumn
 public void removeColumn(Object columnIdentifier)
Remove the column with an unique identifier that matches the parameter columnIdentifier, using the equals() method.

Parameters:
columnIdentifier - the unique identifier of the column being removed
Throws: IllegalArgumentException
if columnIdentifier is null
 o addRow
 public void addRow(Vector rowData)
Add a row to the end of the model. The new row will contain null values unless rowData is specified.

Parameters:
rowData - optional data of the row being added
 o insertRow
 public void insertRow(int rowIndex,
                       Vector rowData)
Insert a row at rowIndex in the model. The new row will contain null values unless rowData is specified.

Parameters:
rowIndex - the row index of the row to be inserted
rowData - optional data of the row being added
Throws: ArrayIndexOutOfBoundsException
if the rowIndex was invalid.
 o moveRow
 public void moveRow(int fromIndex,
                     int range,
                     int toIndex)
 o removeRow
 public void removeRow(int rowIndex)
Remove the row at rowIndex from the model.

Parameters:
rowIndex - the row index of the row to be removed
Throws: ArrayIndexOutOfBoundsException
if the rowIndex was invalid.
 o getRowCount
 public int getRowCount()
Returns:
the number of rows in the model.
Overrides:
getRowCount in class AbstractTableModel
 o getValueAt
 public Object getValueAt(Object columnIdentifier,
                          int rowIndex)
Parameters:
columnIdentifier - the column whose value is to be looked up
rowIndex - the row whose value is to be looked up
Returns:
the object value for the record in the column with the unique identifier columnIdentifier at rowIndex.
Throws: ArrayIndexOutOfBoundsException
if an invalid row index or columnIdentifier was given.
Overrides:
getValueAt in class AbstractTableModel
 o setValueAt
 public void setValueAt(Object aValue,
                        Object columnIdentifier,
                        int rowIndex)
Sets the object value for the cell in the column with the unique identifier ColumnIdentifier at rowIndex. aValue is the new value.

Parameters:
aValue - the new value. This can be null.
columnIdentifier - the column whose value is to be changed
rowIndex - the row whose value is to be changed
Throws: ArrayIndexOutOfBoundsException
if an invalid row index or columnIdentifier was given.
Overrides:
setValueAt in class AbstractTableModel
 o generateIds
 protected static Vector generateIds(int numColumns)

All Packages  Class Hierarchy  This Package  Previous  Next  Index