Contents | Package | Class | Tree | Deprecated | Index | Help Java 1.2 Beta 3
PREV | NEXT SHOW LISTS | HIDE LISTS

Class java.awt.swing.table.DefaultTableModel

java.lang.Object
    |
    +----java.awt.swing.table.AbstractTableModel
            |
            +----java.awt.swing.table.DefaultTableModel

public class DefaultTableModel
extends AbstractTableModel
implements Serializable
This is an implementation of TableModel that uses a Vector of Vectors to store the cell value objects.

Note:
The DefaultTableModel'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 TableColumnModel.

Warning: serialized objects of this class will not be compatible with future swing releases. The current serialization support is appropriate for short term storage or RMI between Swing1.0 applications. It will not be possible to load serialized Swing1.0 objects with future releases of Swing. The JDK1.2 release of Swing will be the compatibility baseline for the serialized form of Swing objects.

See Also:
TableModel, getDataVector()

Field Summary
Vector  columnIdentifiers
The Vector column identifiers
Vector  dataVector
The Vector of Vector of Object values
 
Fields inherited from class java.awt.swing.table.AbstractTableModel
 listenerList
 

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

Method Summary
void  addColumn(Object columnIdentifier)
Add a column to the model.
void  addColumn(Object columnIdentifier, Vector columnData)
Add a column to the model.
void  addColumn(Object columnIdentifier, Object[] columnData)
Add a column to the model.
void  addRow(Vector rowData)
Add a row to the end of the model.
void  addRow(Object[] rowData)
Add a row to the end of the model.
static Vector  convertToVector(Object[] anArray)
Returns a Vector that contains the same objects as the array
static Vector  convertToVector(Object[][] anArray)
Returns a Vector of Vectors that contains the same objects as the array
int  getColumnCount()
 
String  getColumnName(int column)
 
Vector  getDataVector()
This returns the Vector of Vectors that contains the table's data values.
int  getRowCount()
 
Object  getValueAt(int row, int column)
Returns an attribute value for the cell at row and column.
void  insertRow(int row, Vector rowData)
Insert a row at row in the model.
void  insertRow(int row, Object[] rowData)
Insert a row at row in the model.
boolean  isCellEditable(int row, int column)
Returns true if the cell at row and column is editable.
void  moveRow(int startIndex, int endIndex, int toIndex)
Moves one or more rows starting at startIndex to endIndex in the model to the toIndex.
void  newDataAvailable(TableModelEvent event)
This method is used to informed the model that the model's dataVector has been changed directly.
void  newRowsAdded(TableModelEvent event)
This method will recalculate numRows, it will also make sure the new rows have the correct number of columns.
void  removeRow(int row)
Remove the row at row from the model.
void  rowsRemoved(TableModelEvent event)
This method will recalculate numRows, then it will send out the tableRowsRemoved() notification message to all the listeners.
void  setColumnIdentifiers(Vector newIdentifiers)
Replaces the column identifiers in the model.
void  setColumnIdentifiers(Object[] newIdentifiers)
Replaces the column identifiers in the model.
void  setDataVector(Vector newData, Vector columnIds)
This replaces the current dataVector instance variable with the parameter newData.
void  setDataVector(Object[][] newData, Object[] columnIds)
This replaces the value in the dataVector instance variable with the values in the array newData.
void  setNumRows(int newSize)
Sets the number of rows in the model.
void  setValueAt(Object aValue, int row, int column)
Sets the object value for the cell at column and row.
 
Methods inherited from class java.awt.swing.table.AbstractTableModel
 addTableModelListener, findColumn, fireTableCellUpdated, fireTableChanged, fireTableDataChanged, fireTableRowsDeleted, fireTableRowsInserted, fireTableRowsUpdated, fireTableStructureChanged, getColumnClass, getColumnName, isCellEditable, removeTableModelListener, setValueAt
 
Methods inherited from class java.lang.Object
 clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

dataVector

protected Vector dataVector
The Vector of Vector of Object values

columnIdentifiers

protected Vector columnIdentifiers
The Vector column identifiers
Constructor Detail

DefaultTableModel

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

DefaultTableModel

public DefaultTableModel(int numColumns,
                         int numRows)
Constructs a DefaultTableModel with numColumns and numRows of null object values.
Parameters:
numColumns - The number of columns the table holds
numRows - The number of rows the table holds
See Also:
setValueAt()

DefaultTableModel

public DefaultTableModel(Vector columnIds,
                         int numRows)
Constructs a DefaultTableModel with as many columns as there are elements in columnIds and numRows of null object values. Each column's identifier object will be taken from the columnIds vector.
Parameters:
columnIds - Vector containing 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()

DefaultTableModel

public DefaultTableModel(Object[] columnIds,
                         int numRows)
Constructs a DefaultTableModel with as many columns as there are elements in columnIds and numRows of null object values. Each column's identifier object will be taken from the columnIds array.
Parameters:
columnIds - Array containing 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()

DefaultTableModel

public DefaultTableModel(Vector data,
                         Vector columnIds)
Constructs a DefaultTableModel 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
columnIds - Vector containing identifier objects to be used to identify the columns.
Throws:
IllegalArgumentException - see setDataVector().
See Also:
getDataVector(), setDataVector()

DefaultTableModel

public DefaultTableModel(Object[][] data,
                         Object[] columnIds)
Constructs a DefaultTableModel and initialize the table by passing data and columnIds to the setDataVector() method. The first index in the Object[][] is the row index and the second is the column index.
Parameters:
data - The data of the table
columnIds - Array containing identifier objects to be used to identify the columns.
Throws:
IllegalArgumentException - see setDataVector().
See Also:
getDataVector(), setDataVector()
Method Detail

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 row 1, column 5

((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. Depending on which one of the three best describes the changes made. 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 listeners of this model.

See Also:
newDataAvailable(), newRowsAdded(), rowsRemoved(), setDataVector()

setDataVector

public void setDataVector(Vector newData,
                          Vector columnIds)
This replaces the current dataVector instance variable with the parameter newData. columnIds are the 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 column identifiers for the new data vector
Throws:
IllegalArgumentException - if newData is null or if the number of columns in newData does not equal the number of the column identifiers in columnIds.
See Also:
newDataAvailable(), getDataVector()

setDataVector

public void setDataVector(Object[][] newData,
                          Object[] columnIds)
This replaces the value in the dataVector instance variable with the values in the array newData. The first index in the Object[][] array is the row index and the second is the column index. columnIds are the identifiers of the new columns. Finally, this method calls newDataAvailable() to generate the appropriate notification.
Parameters:
newData - The new data array
columnIds - The corresponding column identifiers for the new data array
Throws:
IllegalArgumentException - if newData is null or if the number of columns in newData does not equal the number of the column identifiers in columnIds.
See Also:
newDataAvailable(), getDataVector()

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 extent of the update. This method will send the tableChanged() notification message to all the 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.

See Also:
getDataVector(), newRowsAdded(), rowsRemoved()

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 tableChanged() notification message to all the listeners.
See Also:
getDataVector()

rowsRemoved

public void rowsRemoved(TableModelEvent event)
This method will recalculate numRows, then it will send out the tableRowsRemoved() notification message to all the listeners.
Throws:
IllegalArgumentException - if event is null
See Also:
getDataVector()

setColumnIdentifiers

public void setColumnIdentifiers(Vector newIdentifiers)
Replaces the column identifiers in the model.
Parameters:
newIdentifiers - Vector of column identifiers. A null means setting the model to zero columns
See Also:
setNumRows()

setColumnIdentifiers

public void setColumnIdentifiers(Object[] newIdentifiers)
Replaces the column identifiers in the model. If the number of newIdentifiers 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:
newIdentifiers - Array of column identifiers. A null means setting the model to zero columns
See Also:
setNumRows()

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
See Also:
setColumnIdentifiers()

addColumn

public void addColumn(Object columnIdentifier)
Add a column to the model. The new column will have the idenitifier columnIdentifier. This method will send a tableChanged() notification message to all the listeners. This method is a cover for addColumn(Object, Vector) which uses null as the data vector.
Parameters:
columnIdentifier - the identifier of the column being added
Throws:
IllegalArgumentException - if columnIdentifier is null

addColumn

public void addColumn(Object columnIdentifier,
                      Vector columnData)
Add a column to the model. The new column will have the 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. This method will send a tableChanged() notification message to all the listeners.
Parameters:
columnIdentifier - the identifier of the column being added
columnData - optional data of the column being added
Throws:
IllegalArgumentException - if columnIdentifier is null

addColumn

public void addColumn(Object columnIdentifier,
                      Object[] columnData)
Add a column to the model. The new column will have the idenitifier columnIdentifier. columnData is the optional array 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. This method will send a tableChanged() notification message to all the listeners.
Parameters:
columnIdentifier - the identifier of the column being added
columnData - optional data of the column being added
Throws:
IllegalArgumentException - if columnIdentifier is null

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. Notification of the row being added will be generated.
Parameters:
rowData - optional data of the row being added

addRow

public void addRow(Object[] rowData)
Add a row to the end of the model. The new row will contain null values unless rowData is specified. Notification of the row being added will be generated.
Parameters:
rowData - optional data of the row being added

insertRow

public void insertRow(int row,
                      Vector rowData)
Insert a row at row in the model. The new row will contain null values unless rowData is specified. Notification of the row being added will be generated.
Parameters:
row - the row index of the row to be inserted
rowData - optional data of the row being added
Throws:
ArrayIndexOutOfBoundsException - if the row was invalid.

insertRow

public void insertRow(int row,
                      Object[] rowData)
Insert a row at row in the model. The new row will contain null values unless rowData is specified. Notification of the row being added will be generated.
Parameters:
row - the row index of the row to be inserted
rowData - optional data of the row being added
Throws:
ArrayIndexOutOfBoundsException - if the row was invalid.

moveRow

public void moveRow(int startIndex,
                    int endIndex,
                    int toIndex)
Moves one or more rows starting at startIndex to endIndex in the model to the toIndex. This method will send a tableChanged() notification message to all the listeners.

Examples of moves:

1. moveRow(1,3,5);

a|B|C|D|e|f|g|h|i|j|k - before a|e|f|B|C|D|g|h|i|j|k - after 2. moveRow(6,7,1);

a|b|c|d|e|f|G|H|i|j|k - before a|G|H|b|c|d|e|f|i|j|k - after

Parameters:
startIndex - the starting row index to be moved
endIndex - the ending row index to be moved
toIndex - the destination of the rows to be moved
Throws:
ArrayIndexOutOfBoundsException - if any of the indices are out of range. Or if endIndex is less than startIndex.

removeRow

public void removeRow(int row)
Remove the row at row from the model. Notification of the row being removed, rowsRemoved(), will be sent to all the listeners.
Parameters:
row - the row index of the row to be removed
Throws:
ArrayIndexOutOfBoundsException - if the row was invalid.

getRowCount

public int getRowCount()
Returns:
the number of rows in the model.

getColumnCount

public int getColumnCount()
Returns:
the number of columns in the model.

getColumnName

public String getColumnName(int column)
Returns:
a name for this column using the string value of the appropriate member in columnIdentfiers. If columnIdentfiers is null or does not have and entry for this index return the default name provided by the superclass.
Overrides:
getColumnName in class AbstractTableModel

isCellEditable

public boolean isCellEditable(int row,
                              int column)
Returns true if the cell at row and column is editable. Otherwise, the setValueAt() on the cell will not change the value of that cell.
Parameters:
row - the row whose value is to be looked up
column - the column whose value is to be looked up
Returns:
true if the cell is editable.
Overrides:
isCellEditable in class AbstractTableModel
See Also:
setValueAt()

getValueAt

public Object getValueAt(int row,
                         int column)
Returns an attribute value for the cell at row and column.
Parameters:
row - the row whose value is to be looked up
column - the column whose value is to be looked up
Returns:
the value Object at the specified cell
Throws:
ArrayIndexOutOfBoundsException - if an invalid row or column was given.

setValueAt

public void setValueAt(Object aValue,
                       int row,
                       int column)
Sets the object value for the cell at column and row. aValue is the new value. This method will generate a tableChanged() notification.
Parameters:
aValue - the new value. This can be null.
row - the row whose value is to be changed
column - the column whose value is to be changed
Throws:
ArrayIndexOutOfBoundsException - if an invalid row or column was given.
Overrides:
setValueAt in class AbstractTableModel

convertToVector

protected static Vector convertToVector(Object[] anArray)
Returns a Vector that contains the same objects as the array

convertToVector

protected static Vector convertToVector(Object[][] anArray)
Returns a Vector of Vectors that contains the same objects as the array

Contents | Package | Class | Tree | Deprecated | Index | Help Java 1.2 Beta 3
PREV | NEXT SHOW LISTS | HIDE LISTS

Submit a bug or feature
Submit comments/suggestions about new javadoc look.
Java is a trademark or registered trademark of Sun Microsystems, Inc. in the US and other countries.
Copyright 1993-1998 Sun Microsystems, Inc. 901 San Antonio Road, Palo Alto, California, 94303, U.S.A. All Rights Reserved.