JavaTM 2 Platform
Standard Edition

javax.swing.table
Class DefaultTableModel

java.lang.Object
  |
  +--javax.swing.table.AbstractTableModel
        |
        +--javax.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 applications running the same version of Swing. A future release of Swing will provide support for long term persistence.

See Also:
TableModel, getDataVector(), Serialized Form

Field Summary
protected  Vector columnIdentifiers
          The Vector of column identifiers
protected  Vector dataVector
          The Vector of Vector of Object values
 
Fields inherited from class javax.swing.table.AbstractTableModel
listenerList
 
Constructor Summary
DefaultTableModel()
          Constructs a default DefaultTableModel which is a table of zero columns and zero rows.
DefaultTableModel(int numRows, int numColumns)
          Constructs a DefaultTableModel with numRows and numColumns of null object values.
DefaultTableModel(Object[][] data, Object[] columnNames)
          Constructs a DefaultTableModel and initializes the table by passing data and columnNames to the setDataVector() method.
DefaultTableModel(Object[] columnNames, int numRows)
          Constructs a DefaultTableModel with as many columns as there are elements in columnNames and numRows of null object values.
DefaultTableModel(Vector columnNames, int numRows)
          Constructs a DefaultTableModel with as many columns as there are elements in columnNames and numRows of null object values.
DefaultTableModel(Vector data, Vector columnNames)
          Constructs a DefaultTableModel and initializes the table by passing data and columnNames to the setDataVector() method.
 
Method Summary
 void addColumn(Object columnName)
          Add a column to the model.
 void addColumn(Object columnName, Object[] columnData)
          Adds a column to the model with name columnName.
 void addColumn(Object columnName, Vector columnData)
          Add a column to the model.
 void addRow(Object[] rowData)
          Add a row to the end of the model.
 void addRow(Vector rowData)
          Add a row to the end of the model.
protected static Vector convertToVector(Object[] anArray)
          Returns a Vector that contains the same objects as the array
protected static Vector convertToVector(Object[][] anArray)
          Returns a Vector of Vectors that contains the same objects as the array
 int getColumnCount()
          Returns the number of columns in this data table.
 String getColumnName(int column)
          Returns the column name.
 Vector getDataVector()
          This returns the Vector of Vectors that contains the table's data values.
 int getRowCount()
          Returns the number of rows in this data table.
 Object getValueAt(int row, int column)
          Returns an attribute value for the cell at row and column.
 void insertRow(int row, Object[] rowData)
          Insert a row at row in the model.
 void insertRow(int row, Vector 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)
          Equivalent to fireTableChanged.
 void newRowsAdded(TableModelEvent event)
          This method will 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)
          Equivalent to fireTableChanged().
 void setColumnIdentifiers(Object[] newIdentifiers)
          Replaces the column identifiers in the model.
 void setColumnIdentifiers(Vector newIdentifiers)
          Replaces the column identifiers in the model.
 void setDataVector(Object[][] newData, Object[] columnNames)
          This replaces the value in the dataVector instance variable with the values in the array newData.
 void setDataVector(Vector newData, Vector columnNames)
          This replaces the current dataVector instance variable with the new Vector of rows, 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 javax.swing.table.AbstractTableModel
addTableModelListener, findColumn, fireTableCellUpdated, fireTableChanged, fireTableDataChanged, fireTableRowsDeleted, fireTableRowsInserted, fireTableRowsUpdated, fireTableStructureChanged, getColumnClass, removeTableModelListener
 
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 of 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 numRows,
                         int numColumns)
Constructs a DefaultTableModel with numRows and numColumns of null object values.
Parameters:
numRows - The number of rows the table holds
numColumns - The number of columns the table holds
See Also:
setValueAt(java.lang.Object, int, int)

DefaultTableModel

public DefaultTableModel(Vector columnNames,
                         int numRows)
Constructs a DefaultTableModel with as many columns as there are elements in columnNames and numRows of null object values. Each column's name will be taken from the columnNames vector.
Parameters:
columnNames - Vector containing the names of the new columns. If this null then the model has no columns
numRows - The number of rows the table holds
See Also:
setDataVector(java.util.Vector, java.util.Vector), setValueAt(java.lang.Object, int, int)

DefaultTableModel

public DefaultTableModel(Object[] columnNames,
                         int numRows)
Constructs a DefaultTableModel with as many columns as there are elements in columnNames and numRows of null object values. Each column's name will be taken from the columnNames array.
Parameters:
columnNames - Array containing the names of the new columns. If this null then the model has no columns
numRows - The number of rows the table holds
See Also:
setDataVector(java.util.Vector, java.util.Vector), setValueAt(java.lang.Object, int, int)

DefaultTableModel

public DefaultTableModel(Vector data,
                         Vector columnNames)
Constructs a DefaultTableModel and initializes the table by passing data and columnNames to the setDataVector() method.
Parameters:
data - The data of the table
columnNames - Vector containing the names of the new columns.
See Also:
getDataVector(), setDataVector(java.util.Vector, java.util.Vector)

DefaultTableModel

public DefaultTableModel(Object[][] data,
                         Object[] columnNames)
Constructs a DefaultTableModel and initializes the table by passing data and columnNames 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
columnNames - The names of the columns.
See Also:
getDataVector(), setDataVector(java.util.Vector, java.util.Vector)
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. 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 one of the notification methods. The notification methods will generate the appropriate TableModelListener messages to notify the JTable and any other listeners of this model.

See Also:
newDataAvailable(javax.swing.event.TableModelEvent), newRowsAdded(javax.swing.event.TableModelEvent), setDataVector(java.util.Vector, java.util.Vector)

setDataVector

public void setDataVector(Vector newData,
                          Vector columnNames)
This replaces the current dataVector instance variable with the new Vector of rows, newData. columnNames are the names of the new columns. The first name in columnNames is mapped to column 0 in newData. Each row in newData is adjusted to match the number of columns in columnNames either by truncating the Vector if it is too long, or adding null values if it is too short.

Parameters:
newData - The new data vector
columnNames - The names of the columns
See Also:
newDataAvailable(javax.swing.event.TableModelEvent), getDataVector()

setDataVector

public void setDataVector(Object[][] newData,
                          Object[] columnNames)
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. columnNames are the names of the new columns.
See Also:
setDataVector(Vector, Vector)

newDataAvailable

public void newDataAvailable(TableModelEvent event)
Equivalent to fireTableChanged.

newRowsAdded

public void newRowsAdded(TableModelEvent event)
This method will make sure the new rows have the correct number of columns. It does so using the setSize method in Vector which truncates Vectors which are too long, and appends nulls if they are too short. This method also sends out a tableChanged() notification message to all the listeners.
See Also:
getDataVector()

rowsRemoved

public void rowsRemoved(TableModelEvent event)
Equivalent to fireTableChanged().

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(int)

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(int)

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(java.util.Vector)

addColumn

public void addColumn(Object columnName)
Add a column to the model. The new column will have the idenitifier columnName. 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:
columnName - the identifier of the column being added
Throws:
IllegalArgumentException - if columnName is null

addColumn

public void addColumn(Object columnName,
                      Vector columnData)
Add a column to the model. The new column will have the idenitifier columnName. 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:
columnName - the identifier of the column being added
columnData - optional data of the column being added
Throws:
IllegalArgumentException - if columnName is null

addColumn

public void addColumn(Object columnName,
                      Object[] columnData)
Adds a column to the model with name columnName.
See Also:
addColumn(Object, Vector)

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 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 this data table.
Returns:
the number of rows in the model

getColumnCount

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

getColumnName

public String getColumnName(int column)
Returns the column name.
Overrides:
getColumnName in class AbstractTableModel
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.

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.
Overrides:
isCellEditable in class AbstractTableModel
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.
See Also:
setValueAt(java.lang.Object, int, int)

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.
Overrides:
setValueAt in class AbstractTableModel
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.

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

JavaTM 2 Platform
Standard Edition

Submit a bug or feature
Java, Java 2D, and JDBC are a trademarks or registered trademarks of Sun Microsystems, Inc. in the US and other countries.
Copyright 1993-1999 Sun Microsystems, Inc. 901 San Antonio Road,
Palo Alto, California, 94303, U.S.A. All Rights Reserved.