Class DefaultTableModel
- All Implemented Interfaces:
Serializable, TableModel
public class DefaultTableModel extends AbstractTableModel implements Serializable
TableModel that uses a Vector of Vectors to store the cell value objects. Warning: DefaultTableModel returns a column class of Object. When DefaultTableModel is used with a TableRowSorter this will result in extensive use of toString, which for non-String data types is expensive. If you use DefaultTableModel with a TableRowSorter you are strongly encouraged to override getColumnClass to return the appropriate type.
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. As of 1.4, support for long term storage of all JavaBeans has been added to the java.beans package. Please see XMLEncoder.
- See Also:
Field Summary
| Modifier and Type | Field | Description |
|---|---|---|
protected Vector |
columnIdentifiers |
The Vector of column identifiers. |
protected Vector |
dataVector |
The Vector of Vectors of Object values. |
Fields declared in class AbstractTableModel
listenerList | Modifier and Type | Field | Description |
|---|---|---|
protected EventListenerList |
listenerList |
List of listeners |
Constructor Summary
| Constructor | Description |
|---|---|
DefaultTableModel() |
Constructs a default DefaultTableModel which is a table of zero columns and zero rows. |
DefaultTableModel |
Constructs a DefaultTableModel with rowCount and columnCount of null object values. |
DefaultTableModel |
Constructs a DefaultTableModel and initializes the table by passing data and columnNames to the setDataVector method. |
DefaultTableModel |
Constructs a DefaultTableModel with as many columns as there are elements in columnNames and rowCount of null object values. |
DefaultTableModel |
Constructs a DefaultTableModel with as many columns as there are elements in columnNames and rowCount of null object values. |
DefaultTableModel |
Constructs a DefaultTableModel and initializes the table by passing data and columnNames to the setDataVector method. |
Method Summary
| Modifier and Type | Method | Description |
|---|---|---|
void |
addColumn |
Adds a column to the model. |
void |
addColumn |
Adds a column to the model. |
void |
addColumn |
Adds a column to the model. |
void |
addRow |
Adds a row to the end of the model. |
void |
addRow |
Adds a row to the end of the model. |
protected static Vector |
convertToVector |
Returns a vector that contains the same objects as the array. |
protected static Vector |
convertToVector |
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 |
Returns the column name. |
Vector |
getDataVector() |
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 |
Returns an attribute value for the cell at row and column. |
void |
insertRow |
Inserts a row at row in the model. |
void |
insertRow |
Inserts a row at row in the model. |
boolean |
isCellEditable |
Returns true regardless of parameter values. |
void |
moveRow |
Moves one or more rows from the inclusive range start to end to the to position in the model. |
void |
newDataAvailable |
Equivalent to fireTableChanged. |
void |
newRowsAdded |
Ensures that the new rows have the correct number of columns. |
void |
removeRow |
Removes the row at row from the model. |
void |
rowsRemoved |
Equivalent to fireTableChanged. |
void |
setColumnCount |
Sets the number of columns in the model. |
void |
setColumnIdentifiers |
Replaces the column identifiers in the model. |
void |
setColumnIdentifiers |
Replaces the column identifiers in the model. |
void |
setDataVector |
Replaces the value in the dataVector instance variable with the values in the array dataVector. |
void |
setDataVector |
Replaces the current dataVector instance variable with the new Vector of rows, dataVector. |
void |
setNumRows |
Obsolete as of Java 2 platform v1.3. |
void |
setRowCount |
Sets the number of rows in the model. |
void |
setValueAt |
Sets the object value for the cell at column and row. |
Methods declared in class AbstractTableModel
addTableModelListener, findColumn, fireTableCellUpdated, fireTableChanged, fireTableDataChanged, fireTableRowsDeleted, fireTableRowsInserted, fireTableRowsUpdated, fireTableStructureChanged, getColumnClass, getListeners, getTableModelListeners, removeTableModelListener | Modifier and Type | Method | Description |
|---|---|---|
void |
addTableModelListener |
Adds a listener to the list that's notified each time a change to the data model occurs. |
int |
findColumn |
Returns a column given its name. |
void |
fireTableCellUpdated |
Notifies all listeners that the value of the cell at [row, column] has been updated. |
void |
fireTableChanged |
Forwards the given notification event to all TableModelListeners that registered themselves as listeners for this table model. |
void |
fireTableDataChanged() |
Notifies all listeners that all cell values in the table's rows may have changed. |
void |
fireTableRowsDeleted |
Notifies all listeners that rows in the range [firstRow, lastRow], inclusive, have been deleted. |
void |
fireTableRowsInserted |
Notifies all listeners that rows in the range [firstRow, lastRow], inclusive, have been inserted. |
void |
fireTableRowsUpdated |
Notifies all listeners that rows in the range [firstRow, lastRow], inclusive, have been updated. |
void |
fireTableStructureChanged() |
Notifies all listeners that the table's structure has changed. |
Class |
getColumnClass |
Returns Object.class regardless of columnIndex. |
<T extends EventListener> |
getListeners |
Returns an array of all the objects currently registered as FooListeners upon this AbstractTableModel. |
TableModelListener[] |
getTableModelListeners() |
Returns an array of all the table model listeners registered on this model. |
void |
removeTableModelListener |
Removes a listener from the list that's notified each time a change to the data model occurs. |
Methods declared in class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait | Modifier and Type | Method | Description |
|---|---|---|
protected Object |
clone() |
Creates and returns a copy of this object. |
boolean |
equals |
Indicates whether some other object is "equal to" this one. |
protected void |
finalize() |
Deprecated, for removal: This API element is subject to removal in a future version. Finalization is deprecated and subject to removal in a future release. |
final Class |
getClass() |
Returns the runtime class of this Object. |
int |
hashCode() |
Returns a hash code value for this object. |
final void |
notify() |
Wakes up a single thread that is waiting on this object's monitor. |
final void |
notifyAll() |
Wakes up all threads that are waiting on this object's monitor. |
String |
toString() |
Returns a string representation of the object. |
final void |
wait() |
Causes the current thread to wait until it is awakened, typically by being notified or interrupted. |
final void |
wait |
Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed. |
final void |
wait |
Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed. |
Field Details
dataVector
protected Vector<Vector> dataVector
Vector of Vectors of Object values.columnIdentifiers
protected Vector columnIdentifiers
Vector of column identifiers.Constructor Details
DefaultTableModel
public DefaultTableModel()
DefaultTableModel which is a table of zero columns and zero rows.DefaultTableModel
public DefaultTableModel(int rowCount, int columnCount)
DefaultTableModel with rowCount and columnCount of null object values.- Parameters:
-
rowCount- the number of rows the table holds -
columnCount- the number of columns the table holds - See Also:
DefaultTableModel
public DefaultTableModel(Vector<?> columnNames, int rowCount)
DefaultTableModel with as many columns as there are elements in columnNames and rowCount of null object values. Each column's name will be taken from the columnNames vector.- Parameters:
-
columnNames-vectorcontaining the names of the new columns; if this isnullthen the model has no columns -
rowCount- the number of rows the table holds - See Also:
DefaultTableModel
public DefaultTableModel(Object[] columnNames, int rowCount)
DefaultTableModel with as many columns as there are elements in columnNames and rowCount of null object values. Each column's name will be taken from the columnNames array.- Parameters:
-
columnNames-arraycontaining the names of the new columns; if this isnullthen the model has no columns -
rowCount- the number of rows the table holds - See Also:
DefaultTableModel
public DefaultTableModel(Vector<? extends Vector> data, Vector<?> columnNames)
DefaultTableModel and initializes the table by passing data and columnNames to the setDataVector method.- Parameters:
-
data- the data of the table, aVectorofVectors ofObjectvalues -
columnNames-vectorcontaining the names of the new columns - See Also:
DefaultTableModel
public DefaultTableModel(Object[][] data, Object[] columnNames)
DefaultTableModel and initializes the table by passing data and columnNames to the setDataVector method. The first index in the Object[][] array 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:
Method Details
getDataVector
public Vector<Vector> getDataVector()
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);
- Returns:
- the vector of vectors containing the tables data values
- See Also:
setDataVector
public void setDataVector(Vector<? extends Vector> dataVector, Vector<?> columnIdentifiers)
dataVector instance variable with the new Vector of rows, dataVector. Each row is represented in dataVector as a Vector of Object values. columnIdentifiers are the names of the new columns. The first name in columnIdentifiers is mapped to column 0 in dataVector. Each row in dataVector is adjusted to match the number of columns in columnIdentifiers either by truncating the Vector if it is too long, or adding null values if it is too short. Note that passing in a null value for dataVector results in unspecified behavior, an possibly an exception.
- Parameters:
-
dataVector- the new data vector -
columnIdentifiers- the names of the columns - See Also:
setDataVector
public void setDataVector(Object[][] dataVector, Object[] columnIdentifiers)
dataVector instance variable with the values in the array dataVector. The first index in the Object[][] array is the row index and the second is the column index. columnIdentifiers are the names of the new columns.- Parameters:
-
dataVector- the new data vector -
columnIdentifiers- the names of the columns - See Also:
newDataAvailable
public void newDataAvailable(TableModelEvent event)
fireTableChanged.- Parameters:
-
event- the change event
newRowsAdded
public void newRowsAdded(TableModelEvent e)
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.- Parameters:
-
e- thisTableModelEventdescribes where the rows were added. Ifnullit assumes all the rows were newly added - See Also:
rowsRemoved
public void rowsRemoved(TableModelEvent event)
fireTableChanged.- Parameters:
-
event- the change event
setNumRows
public void setNumRows(int rowCount)
setRowCount instead.- Parameters:
-
rowCount- the new number of rows
setRowCount
public void setRowCount(int rowCount)
rowCount and greater are discarded.- Parameters:
-
rowCount- number of rows in the model - Since:
- 1.3
- See Also:
addRow
public void addRow(Vector<?> rowData)
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)
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)
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)
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 start, int end, int to)
start to end to the to position in the model. After the move, the row that was at index start will be at index to. 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|g|h|B|C|D|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:
-
start- the starting row index to be moved -
end- the ending row index to be moved -
to- the destination of the rows to be moved - Throws:
-
ArrayIndexOutOfBoundsException- if any of the elements would be moved out of the table's range
removeRow
public void removeRow(int row)
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
setColumnIdentifiers
public void setColumnIdentifiers(Vector<?> columnIdentifiers)
newIdentifiers is greater than the current number of columns, new columns are added to the end of each row in the model. If the number of newIdentifiers is less than the current number of columns, all the extra columns at the end of a row are discarded.- Parameters:
-
columnIdentifiers- vector of column identifiers. Ifnull, set the model to zero columns - See Also:
setColumnIdentifiers
public void setColumnIdentifiers(Object[] newIdentifiers)
newIdentifiers is greater than the current number of columns, new columns are added to the end of each row in the model. If the number of newIdentifiers 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. Ifnull, set the model to zero columns - See Also:
setColumnCount
public void setColumnCount(int columnCount)
null cell values. If the new size is less than the current size, all columns at index columnCount and greater are discarded.- Parameters:
-
columnCount- the new number of columns in the model - Since:
- 1.3
- See Also:
addColumn
public void addColumn(Object columnName)
columnName, which may be null. 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
addColumn
public void addColumn(Object columnName, Vector<?> columnData)
columnName, which may be null. 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
addColumn
public void addColumn(Object columnName, Object[] columnData)
columnName. 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:
-
columnName- identifier of the newly created column -
columnData- new data to be added to the column - See Also:
getRowCount
public int getRowCount()
- Specified by:
-
getRowCountin interfaceTableModel - Returns:
- the number of rows in the model
- See Also:
getColumnCount
public int getColumnCount()
- Specified by:
-
getColumnCountin interfaceTableModel - Returns:
- the number of columns in the model
- See Also:
getColumnName
public String getColumnName(int column)
- Specified by:
-
getColumnNamein interfaceTableModel - Overrides:
-
getColumnNamein classAbstractTableModel - Parameters:
-
column- the column being queried - Returns:
- a name for this column using the string value of the appropriate member in
columnIdentifiers. IfcolumnIdentifiersdoes not have an entry for this index, returns the default name provided by the superclass.
isCellEditable
public boolean isCellEditable(int row, int column)
- Specified by:
-
isCellEditablein interfaceTableModel - Overrides:
-
isCellEditablein classAbstractTableModel - Parameters:
-
row- the row whose value is to be queried -
column- the column whose value is to be queried - Returns:
- true
- See Also:
getValueAt
public Object getValueAt(int row, int column)
row and column.- Specified by:
-
getValueAtin interfaceTableModel - Parameters:
-
row- the row whose value is to be queried -
column- the column whose value is to be queried - 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)
column and row. aValue is the new value. This method will generate a tableChanged notification.- Specified by:
-
setValueAtin interfaceTableModel - Overrides:
-
setValueAtin classAbstractTableModel - 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 - See Also:
convertToVector
protected static Vector<Object> convertToVector(Object[] anArray)
- Parameters:
-
anArray- the array to be converted - Returns:
- the new vector; if
anArrayisnull, returnsnull
convertToVector
protected static Vector<Vector<Object>> convertToVector(Object[][] anArray)
- Parameters:
-
anArray- the double array to be converted - Returns:
- the new vector of vectors; if
anArrayisnull, returnsnull
© 1993, 2025, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.