javax.swing.table

Class DefaultTableModel

Implemented Interfaces:
Serializable, TableModel

public class DefaultTableModel
extends AbstractTableModel
implements Serializable

A two dimensional data structure used to store Object instances, usually for display in a JTable component.
See Also:
Serialized Form

Field Summary

protected Vector<T>
columnIdentifiers
Storage for the column identifiers.
protected Vector<T>
dataVector
Storage for the rows in the table (each row is itself a Vector).

Fields inherited from class javax.swing.table.AbstractTableModel

listenerList

Constructor Summary

DefaultTableModel()
Creates an empty table with zero rows and zero columns.
DefaultTableModel(int numRows, int numColumns)
Creates a new table with the specified number of rows and columns.
DefaultTableModel(Object[] columnNames, int numRows)
Creates a new table with the specified column names and row count.
DefaultTableModel(Object[][] data, Object[] columnNames)
Creates a new table with the specified data values and column names.
DefaultTableModel(Vector<T> columnNames, int numRows)
Creates a new table with the specified column names and number of rows.
DefaultTableModel(Vector<T> data, Vector<T> columnNames)
Creates a new table with the specified data values and column names.

Method Summary

void
addColumn(Object columnName)
Adds a column with the specified name to the table.
void
addColumn(Object columnName, Object[] columnData)
Adds a column with the specified name and data values to the table.
void
addColumn(Object columnName, Vector<T> columnData)
Adds a column with the specified name and data values to the table.
void
addRow(Object[] rowData)
Adds a new row containing the specified data to the table and sends a TableModelEvent to all registered listeners.
void
addRow(Vector<T> rowData)
Adds a new row containing the specified data to the table and sends a TableModelEvent to all registered listeners.
protected static Vector<T>
convertToVector(Object[] data)
Converts the data array to a Vector.
protected static Vector<T>
convertToVector(Object[][] data)
Converts the data array to a Vector of rows.
int
getColumnCount()
Returns the number of columns in the model.
String
getColumnName(int column)
Get the name of the column.
Vector<T>
getDataVector()
Returns the vector containing the row data for the table.
int
getRowCount()
Returns the number of rows in the model.
Object
getValueAt(int row, int column)
Returns the value at the specified cell in the table.
void
insertRow(int row, Object[] rowData)
Inserts a new row into the table.
void
insertRow(int row, Vector<T> rowData)
Inserts a new row into the table.
boolean
isCellEditable(int row, int column)
Returns true if the specified cell can be modified, and false otherwise.
void
moveRow(int startIndex, int endIndex, int toIndex)
Moves the rows from startIndex to endIndex (inclusive) to the specified row.
void
newDataAvailable(TableModelEvent event)
Sends the specified event to all registered listeners.
void
newRowsAdded(TableModelEvent event)
Sends the specified event to all registered listeners.
void
removeRow(int row)
Removes a row from the table and sends a TableModelEvent to all registered listeners.
void
rowsRemoved(TableModelEvent event)
Sends the specified event to all registered listeners.
void
setColumnCount(int columnCount)
Sets the number of columns in the table.
void
setColumnIdentifiers(Object[] columnIdentifiers)
Sets the column identifiers, updates the data rows (truncating or padding each row with null values) to match the number of columns, and sends a TableModelEvent to all registered listeners.
void
setColumnIdentifiers(Vector<T> columnIdentifiers)
Sets the column identifiers, updates the data rows (truncating or padding each row with null values) to match the number of columns, and sends a TableModelEvent to all registered listeners.
void
setDataVector(Object[][] data, Object[] columnNames)
Sets the data and column identifiers for the table.
void
setDataVector(Vector<T> data, Vector<T> columnNames)
Sets the data and column identifiers for the table.
void
setNumRows(int numRows)
This method is obsolete, use setRowCount(int) instead.
void
setRowCount(int rowCount)
Sets the number of rows in the table.
void
setValueAt(Object value, int row, int column)
Sets the value for the specified cell in the table and sends a TableModelEvent to all registered listeners.

Methods inherited from class javax.swing.table.AbstractTableModel

addTableModelListener, extends EventListener> T[] getListeners, findColumn, fireTableCellUpdated, fireTableChanged, fireTableDataChanged, fireTableRowsDeleted, fireTableRowsInserted, fireTableRowsUpdated, fireTableStructureChanged, getColumnClass, getColumnName, getTableModelListeners, isCellEditable, removeTableModelListener, setValueAt

Methods inherited from class java.lang.Object

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Field Details

columnIdentifiers

protected Vector<T> columnIdentifiers
Storage for the column identifiers.

dataVector

protected Vector<T> dataVector
Storage for the rows in the table (each row is itself a Vector).

Constructor Details

DefaultTableModel

public DefaultTableModel()
Creates an empty table with zero rows and zero columns.

DefaultTableModel

public DefaultTableModel(int numRows,
                         int numColumns)
Creates a new table with the specified number of rows and columns. All cells in the table are initially empty (set to null).
Parameters:
numRows - the number of rows.
numColumns - the number of columns.

DefaultTableModel

public DefaultTableModel(Object[] columnNames,
                         int numRows)
Creates a new table with the specified column names and row count.
Parameters:
columnNames - the column names.
numRows - the number of rows.

DefaultTableModel

public DefaultTableModel(Object[][] data,
                         Object[] columnNames)
Creates a new table with the specified data values and column names.
Parameters:
data - the data values.
columnNames - the column names.

DefaultTableModel

public DefaultTableModel(Vector<T> columnNames,
                         int numRows)
Creates a new table with the specified column names and number of rows. The number of columns is determined by the number of column names supplied.
Parameters:
columnNames - the column names.
numRows - the number of rows.

DefaultTableModel

public DefaultTableModel(Vector<T> data,
                         Vector<T> columnNames)
Creates a new table with the specified data values and column names.
Parameters:
data - the data values.
columnNames - the column names.

Method Details

addColumn

public void addColumn(Object columnName)
Adds a column with the specified name to the table. All cell values for the column are initially set to null.
Parameters:
columnName - the column name (null permitted).

addColumn

public void addColumn(Object columnName,
                      Object[] columnData)
Adds a column with the specified name and data values to the table.
Parameters:
columnName - the column name (null permitted).
columnData - the column data.

addColumn

public void addColumn(Object columnName,
                      Vector<T> columnData)
Adds a column with the specified name and data values to the table.
Parameters:
columnName - the column name (null permitted).
columnData - the column data.

addRow

public void addRow(Object[] rowData)
Adds a new row containing the specified data to the table and sends a TableModelEvent to all registered listeners.
Parameters:
rowData - the row data (null permitted).

addRow

public void addRow(Vector<T> rowData)
Adds a new row containing the specified data to the table and sends a TableModelEvent to all registered listeners.
Parameters:
rowData - the row data (null permitted).

convertToVector

protected static Vector<T> convertToVector(Object[] data)
Converts the data array to a Vector.
Parameters:
data - the data array (null permitted).
Returns:
A vector (or null if the data array is null).

convertToVector

protected static Vector<T> convertToVector(Object[][] data)
Converts the data array to a Vector of rows.
Parameters:
data - the data array (null permitted).
Returns:
A vector (or null if the data array is null.

getColumnCount

public int getColumnCount()
Returns the number of columns in the model.
Specified by:
getColumnCount in interface TableModel
Returns:
The column count.

getColumnName

public String getColumnName(int column)
Get the name of the column. If the column has the column identifier set, the return value is the result of the .toString() method call on that identifier. If the identifier is not explicitly set, the returned value is calculated by AbstractTableModel.getColumnName(int).
Specified by:
getColumnName in interface TableModel
Overrides:
getColumnName in interface AbstractTableModel
Parameters:
column - the column index.
Returns:
The column name.

getDataVector

public Vector<T> getDataVector()
Returns the vector containing the row data for the table.
Returns:
The data vector.

getRowCount

public int getRowCount()
Returns the number of rows in the model.
Specified by:
getRowCount in interface TableModel
Returns:
The row count.

getValueAt

public Object getValueAt(int row,
                         int column)
Returns the value at the specified cell in the table.
Specified by:
getValueAt in interface TableModel
Parameters:
row - the row index.
column - the column index.
Returns:
The value (Object, possibly null) at the specified cell in the table.

insertRow

public void insertRow(int row,
                      Object[] rowData)
Inserts a new row into the table.
Parameters:
row - the row index.
rowData - the row data.

insertRow

public void insertRow(int row,
                      Vector<T> rowData)
Inserts a new row into the table.
Parameters:
row - the row index.
rowData - the row data.

isCellEditable

public boolean isCellEditable(int row,
                              int column)
Returns true if the specified cell can be modified, and false otherwise. For this implementation, the method always returns true.
Specified by:
isCellEditable in interface TableModel
Overrides:
isCellEditable in interface AbstractTableModel
Parameters:
row - the row index.
column - the column index.
Returns:
true in all cases.

moveRow

public void moveRow(int startIndex,
                    int endIndex,
                    int toIndex)
Moves the rows from startIndex to endIndex (inclusive) to the specified row.
Parameters:
startIndex - the start row.
endIndex - the end row.
toIndex - the row to move to.

newDataAvailable

public void newDataAvailable(TableModelEvent event)
Sends the specified event to all registered listeners. This method is equivalent to AbstractTableModel.fireTableChanged(TableModelEvent).
Parameters:
event - the event.

newRowsAdded

public void newRowsAdded(TableModelEvent event)
Sends the specified event to all registered listeners. This method is equivalent to AbstractTableModel.fireTableChanged(TableModelEvent).
Parameters:
event - the event.

removeRow

public void removeRow(int row)
Removes a row from the table and sends a TableModelEvent to all registered listeners.
Parameters:
row - the row index.

rowsRemoved

public void rowsRemoved(TableModelEvent event)
Sends the specified event to all registered listeners. This method is equivalent to AbstractTableModel.fireTableChanged(TableModelEvent).
Parameters:
event - the event.

setColumnCount

public void setColumnCount(int columnCount)
Sets the number of columns in the table. Existing rows are truncated or padded with null values to match the new column count. A TableModelEvent is sent to all registered listeners.
Parameters:
columnCount - the column count.

setColumnIdentifiers

public void setColumnIdentifiers(Object[] columnIdentifiers)
Sets the column identifiers, updates the data rows (truncating or padding each row with null values) to match the number of columns, and sends a TableModelEvent to all registered listeners.
Parameters:
columnIdentifiers - the column identifiers.

setColumnIdentifiers

public void setColumnIdentifiers(Vector<T> columnIdentifiers)
Sets the column identifiers, updates the data rows (truncating or padding each row with null values) to match the number of columns, and sends a TableModelEvent to all registered listeners.
Parameters:
columnIdentifiers - the column identifiers.

setDataVector

public void setDataVector(Object[][] data,
                          Object[] columnNames)
Sets the data and column identifiers for the table.
Parameters:
data - the data for the table.
columnNames - the column names.
Throws:
NullPointerException - if either argument is null.

setDataVector

public void setDataVector(Vector<T> data,
                          Vector<T> columnNames)
Sets the data and column identifiers for the table. The data vector contains a Vector for each row in the table - if the number of objects in each row does not match the number of column names specified, the row data is truncated or expanded (by adding null values) as required.
Parameters:
data - the data for the table (a vector of row vectors).
columnNames - the column names.
Throws:
NullPointerException - if either argument is null.

setNumRows

public void setNumRows(int numRows)
This method is obsolete, use setRowCount(int) instead.
Parameters:
numRows - the number of rows.

setRowCount

public void setRowCount(int rowCount)
Sets the number of rows in the table. If rowCount is less than the current number of rows in the table, rows are discarded. If rowCount is greater than the current number of rows in the table, new (empty) rows are added.
Parameters:
rowCount - the row count.

setValueAt

public void setValueAt(Object value,
                       int row,
                       int column)
Sets the value for the specified cell in the table and sends a TableModelEvent to all registered listeners.
Specified by:
setValueAt in interface TableModel
Overrides:
setValueAt in interface AbstractTableModel
Parameters:
value - the value (Object, null permitted).
row - the row index.
column - the column index.

DefaultTableModel.java -- Copyright (C) 2002, 2004, 2005, 2006, Free Software Foundation, Inc. This file is part of GNU Classpath. GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GNU Classpath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Classpath; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination. As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version.