Top Description Fields Constructors Methods
javax.swing.table

public Class DefaultTableModel

extends AbstractTableModel
implements Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable
Annotations
@SuppressWarnings:serial
Imports
java.io.Serializable, java.util.Vector, .Enumeration, javax.swing.event.TableModelEvent

This is an implementation of 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 java.beans.XMLEncoder.

Author
Philip Milne
See Also
TableModel, getDataVector

Field Summary

Modifier and TypeField and Description
protected Vector<E>
columnIdentifiers

The Vector of column identifiers.

protected Vector<Vector<E>>
dataVector

The Vector of Vectors of Object values.

Inherited from javax.swing.table.AbstractTableModel:
listenerList

Constructor Summary

AccessConstructor and Description
public
DefaultTableModel()

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

public
DefaultTableModel(int
the number of rows the table holds
rowCount
,
int
the number of columns the table holds
columnCount
)

Constructs a DefaultTableModel with rowCount and columnCount of null object values.

public
DefaultTableModel(Vector<?>
vector containing the names of the new columns; if this is null then the model has no columns
columnNames
,
int
the number of rows the table holds
rowCount
)

Constructs a DefaultTableModel with as many columns as there are elements in columnNames and rowCount of null object values.

public
DefaultTableModel(Object[]
array containing the names of the new columns; if this is null then the model has no columns
columnNames
,
int
the number of rows the table holds
rowCount
)

Constructs a DefaultTableModel with as many columns as there are elements in columnNames and rowCount of null object values.

public
DefaultTableModel(Vector<? extends Vector<E>>
the data of the table, a Vector of Vectors of Object values
data
,
Vector<?>
vector containing the names of the new columns
columnNames
)

Constructs a DefaultTableModel and initializes the table by passing data and columnNames to the setDataVector method.

public
DefaultTableModel(Object[][]
the data of the table
data
,
Object[]
the names of the columns
columnNames
)

Constructs a DefaultTableModel and initializes the table by passing data and columnNames to the setDataVector method.

Method Summary

Modifier and TypeMethod and Description
public void
addColumn(Object
the identifier of the column being added
columnName
)

Adds a column to the model.

public void
addColumn(Object
the identifier of the column being added
columnName
,
Vector<?>
optional data of the column being added
columnData
)

Adds a column to the model.

public void
addColumn(Object
identifier of the newly created column
columnName
,
Object[]
new data to be added to the column
columnData
)

Adds a column to the model.

public void
addRow(Vector<?>
optional data of the row being added
rowData
)

Adds a row to the end of the model.

public void
addRow(Object[]
optional data of the row being added
rowData
)

Adds a row to the end of the model.

protected static Vector<Object>

Returns:

the new vector; if anArray is null, returns null
convertToVector
(Object[]
the array to be converted
anArray
)

Returns a vector that contains the same objects as the array.

protected static Vector<Vector<Object>>

Returns:

the new vector of vectors; if anArray is null, returns null
convertToVector
(Object[][]
the double array to be converted
anArray
)

Returns a vector of vectors that contains the same objects as the array.

private static int
gcd(int i, int j)

public int

Returns:

the number of columns in the model
getColumnCount
()

Implements javax.swing.table.TableModel.getColumnCount.

Returns the number of columns in this data table.
public String

Returns:

a name for this column using the string value of the appropriate member in columnIdentifiers. If columnIdentifiers does not have an entry for this index, returns the default name provided by the superclass.
getColumnName
(int
the column being queried
column
)

Overrides javax.swing.table.AbstractTableModel.getColumnName.

Implements javax.swing.table.TableModel.getColumnName.

Returns the column name.
public Vector<Vector<E>>

Returns:

the vector of vectors containing the tables data values
getDataVector
()

Returns the Vector of Vectors that contains the table's data values.

public int

Returns:

the number of rows in the model
getRowCount
()

Implements javax.swing.table.TableModel.getRowCount.

Returns the number of rows in this data table.
public Object

Returns:

the value Object at the specified cell
getValueAt
(int
the row whose value is to be queried
row
,
int
the column whose value is to be queried
column
)

Implements javax.swing.table.TableModel.getValueAt.

Returns an attribute value for the cell at row and column.
public void
insertRow(int
the row index of the row to be inserted
row
,
Vector<?>
optional data of the row being added
rowData
)

Inserts a row at row in the model.

public void
insertRow(int
the row index of the row to be inserted
row
,
Object[]
optional data of the row being added
rowData
)

Inserts a row at row in the model.

public boolean

Returns:

true
isCellEditable
(int
the row whose value is to be queried
row
,
int
the column whose value is to be queried
column
)

Overrides javax.swing.table.AbstractTableModel.isCellEditable.

Implements javax.swing.table.TableModel.isCellEditable.

Returns true regardless of parameter values.
private void
justifyRows(int from, int to)

public void
moveRow(int
the starting row index to be moved
start
,
int
the ending row index to be moved
end
,
int
the destination of the rows to be moved
to
)

Moves one or more rows from the inclusive range start to end to the to position in the model.

public void
newDataAvailable(TableModelEvent
the change event
event
)

Equivalent to fireTableChanged.

public void
newRowsAdded(TableModelEvent
this TableModelEvent describes where the rows were added. If null it assumes all the rows were newly added
e
)

Ensures that the new rows have the correct number of columns.

private static <E> Vector<E>
newVector(int size)

private static <E> Vector<E>
public void
removeRow(int
the row index of the row to be removed
row
)

Removes the row at row from the model.

private static <E> void
rotate(Vector<E> v, int a, int b, int shift)

public void
rowsRemoved(TableModelEvent
the change event
event
)

Equivalent to fireTableChanged.

public void
setColumnCount(int
the new number of columns in the model
columnCount
)

Sets the number of columns in the model.

public void
setColumnIdentifiers(Vector<?>
vector of column identifiers. If null, set the model to zero columns
columnIdentifiers
)

Replaces the column identifiers in the model.

public void
setColumnIdentifiers(Object[]
array of column identifiers. If null, set the model to zero columns
newIdentifiers
)

Replaces the column identifiers in the model.

public void
setDataVector(Vector<? extends Vector<E>>
the new data vector
dataVector
,
Vector<?>
the names of the columns
columnIdentifiers
)

Replaces the current dataVector instance variable with the new Vector of rows, dataVector.

public void
setDataVector(Object[][]
the new data vector
dataVector
,
Object[]
the names of the columns
columnIdentifiers
)

Replaces the value in the dataVector instance variable with the values in the array dataVector.

public void
setNumRows(int
the new number of rows
rowCount
)

Obsolete as of Java 2 platform v1.3.

public void
setRowCount(int
number of rows in the model
rowCount
)

Sets the number of rows in the model.

public void
setValueAt(Object
the new value; this can be null
aValue
,
int
the row whose value is to be changed
row
,
int
the column whose value is to be changed
column
)

Overrides javax.swing.table.AbstractTableModel.setValueAt.

Implements javax.swing.table.TableModel.setValueAt.

Sets the object value for the cell at column and row.
Inherited from javax.swing.table.AbstractTableModel:
addTableModelListenerfindColumnfireTableCellUpdatedfireTableChangedfireTableDataChangedfireTableRowsDeletedfireTableRowsInsertedfireTableRowsUpdatedfireTableStructureChangedgetColumnClassgetListenersgetTableModelListenersremoveTableModelListener

Field Detail

columnIdentifiersback to summary
protected Vector<E> columnIdentifiers

The Vector of column identifiers.

Annotations
@SuppressWarnings:rawtypes
dataVectorback to summary
protected Vector<Vector<E>> dataVector

The Vector of Vectors of Object values.

Annotations
@SuppressWarnings:rawtypes

Constructor Detail

DefaultTableModelback to summary
public DefaultTableModel()

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

DefaultTableModelback to summary
public DefaultTableModel(int rowCount, int columnCount)

Constructs a DefaultTableModel with rowCount and columnCount of null object values.

Parameters
rowCount:int

the number of rows the table holds

columnCount:int

the number of columns the table holds

See Also
setValueAt
DefaultTableModelback to summary
public DefaultTableModel(Vector<?> columnNames, int rowCount)

Constructs a 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:Vector<?>

vector containing the names of the new columns; if this is null then the model has no columns

rowCount:int

the number of rows the table holds

See Also
setDataVector, setValueAt
DefaultTableModelback to summary
public DefaultTableModel(Object[] columnNames, int rowCount)

Constructs a 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:Object[]

array containing the names of the new columns; if this is null then the model has no columns

rowCount:int

the number of rows the table holds

See Also
setDataVector, setValueAt
DefaultTableModelback to summary
public DefaultTableModel(Vector<? extends Vector<E>> data, Vector<?> columnNames)

Constructs a DefaultTableModel and initializes the table by passing data and columnNames to the setDataVector method.

Parameters
data:Vector<? extends Vector<E>>

the data of the table, a Vector of Vectors of Object values

columnNames:Vector<?>

vector containing the names of the new columns

Annotations
@SuppressWarnings:rawtypes
See Also
getDataVector, setDataVector
DefaultTableModelback to summary
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[][] array is the row index and the second is the column index.

Parameters
data:Object[][]

the data of the table

columnNames:Object[]

the names of the columns

See Also
getDataVector, setDataVector

Method Detail

addColumnback to summary
public void addColumn(Object columnName)

Adds a column to the model. The new column will have the identifier 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:Object

the identifier of the column being added

addColumnback to summary
public void addColumn(Object columnName, Vector<?> columnData)

Adds a column to the model. The new column will have the identifier 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:Object

the identifier of the column being added

columnData:Vector<?>

optional data of the column being added

Annotations
@SuppressWarnings:unchecked
addColumnback to summary
public void addColumn(Object columnName, Object[] columnData)

Adds a column to the model. The new column will have the identifier 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:Object

identifier of the newly created column

columnData:Object[]

new data to be added to the column

See Also
addColumn(Object, Vector)
addRowback to summary
public void addRow(Vector<?> rowData)

Adds 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:Vector<?>

optional data of the row being added

addRowback to summary
public void addRow(Object[] rowData)

Adds 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:Object[]

optional data of the row being added

convertToVectorback to summary
protected static Vector<Object> convertToVector(Object[] anArray)

Returns a vector that contains the same objects as the array.

Parameters
anArray:Object[]

the array to be converted

Returns:Vector<Object>

the new vector; if anArray is null, returns null

convertToVectorback to summary
protected static Vector<Vector<Object>> convertToVector(Object[][] anArray)

Returns a vector of vectors that contains the same objects as the array.

Parameters
anArray:Object[][]

the double array to be converted

Returns:Vector<Vector<Object>>

the new vector of vectors; if anArray is null, returns null

gcdback to summary
private static int gcd(int i, int j)
getColumnCountback to summary
public int getColumnCount()

Implements javax.swing.table.TableModel.getColumnCount.

Returns the number of columns in this data table.

Returns:int

the number of columns in the model

getColumnNameback to summary
public String getColumnName(int column)

Overrides javax.swing.table.AbstractTableModel.getColumnName.

Implements javax.swing.table.TableModel.getColumnName.

Returns the column name.

Parameters
column:int

Doc from javax.swing.table.AbstractTableModel.getColumnName.

the column being queried

Returns:String

a name for this column using the string value of the appropriate member in columnIdentifiers. If columnIdentifiers does not have an entry for this index, returns the default name provided by the superclass.

getDataVectorback to summary
public Vector<Vector<E>> getDataVector()

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);

Returns:Vector<Vector<E>>

the vector of vectors containing the tables data values

Annotations
@SuppressWarnings:rawtypes
See Also
newDataAvailable, newRowsAdded, setDataVector
getRowCountback to summary
public int getRowCount()

Implements javax.swing.table.TableModel.getRowCount.

Returns the number of rows in this data table.

Returns:int

the number of rows in the model

getValueAtback to summary
public Object getValueAt(int row, int column)

Implements javax.swing.table.TableModel.getValueAt.

Returns an attribute value for the cell at row and column.

Parameters
row:int

the row whose value is to be queried

column:int

the column whose value is to be queried

Returns:Object

the value Object at the specified cell

Exceptions
ArrayIndexOutOfBoundsException:
if an invalid row or column was given
insertRowback to summary
public void insertRow(int row, Vector<?> rowData)

Inserts 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:int

the row index of the row to be inserted

rowData:Vector<?>

optional data of the row being added

Exceptions
ArrayIndexOutOfBoundsException:
if the row was invalid
insertRowback to summary
public void insertRow(int row, Object[] rowData)

Inserts 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:int

the row index of the row to be inserted

rowData:Object[]

optional data of the row being added

Exceptions
ArrayIndexOutOfBoundsException:
if the row was invalid
isCellEditableback to summary
public boolean isCellEditable(int row, int column)

Overrides javax.swing.table.AbstractTableModel.isCellEditable.

Implements javax.swing.table.TableModel.isCellEditable.

Returns true regardless of parameter values.

Parameters
row:int

the row whose value is to be queried

column:int

the column whose value is to be queried

Returns:boolean

true

See Also
setValueAt
justifyRowsback to summary
private void justifyRows(int from, int to)
moveRowback to summary
public void moveRow(int start, int end, int to)

Moves one or more rows from the inclusive range 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:int

the starting row index to be moved

end:int

the ending row index to be moved

to:int

the destination of the rows to be moved

Exceptions
ArrayIndexOutOfBoundsException:
if any of the elements would be moved out of the table's range
newDataAvailableback to summary
public void newDataAvailable(TableModelEvent event)

Equivalent to fireTableChanged.

Parameters
event:TableModelEvent

the change event

newRowsAddedback to summary
public void newRowsAdded(TableModelEvent e)

Ensures that the new rows have the correct number of columns. This is accomplished by 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.

Parameters
e:TableModelEvent

this TableModelEvent describes where the rows were added. If null it assumes all the rows were newly added

See Also
getDataVector
newVectorback to summary
private static <E> Vector<E> newVector(int size)
nonNullVectorback to summary
private static <E> Vector<E> nonNullVector(Vector<E> v)
removeRowback to summary
public void removeRow(int row)

Removes the row at row from the model. Notification of the row being removed will be sent to all the listeners.

Parameters
row:int

the row index of the row to be removed

Exceptions
ArrayIndexOutOfBoundsException:
if the row was invalid
rotateback to summary
private static <E> void rotate(Vector<E> v, int a, int b, int shift)
rowsRemovedback to summary
public void rowsRemoved(TableModelEvent event)

Equivalent to fireTableChanged.

Parameters
event:TableModelEvent

the change event

setColumnCountback to summary
public void setColumnCount(int columnCount)

Sets the number of columns in the model. If the new size is greater than the current size, new columns are added to the end of the model with null cell values. If the new size is less than the current size, all columns at index columnCount and greater are discarded.

Parameters
columnCount:int

the new number of columns in the model

Since
1.3
See Also
setRowCount
setColumnIdentifiersback to summary
public void setColumnIdentifiers(Vector<?> columnIdentifiers)

Replaces the column identifiers in the model. If the number of 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<?>

vector of column identifiers. If null, set the model to zero columns

See Also
setNumRows
setColumnIdentifiersback to summary
public void setColumnIdentifiers(Object[] newIdentifiers)

Replaces the column identifiers in the model. If the number of 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:Object[]

array of column identifiers. If null, set the model to zero columns

See Also
setNumRows
setDataVectorback to summary
public void setDataVector(Vector<? extends Vector<E>> dataVector, Vector<?> columnIdentifiers)

Replaces the current 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:Vector<? extends Vector<E>>

the new data vector

columnIdentifiers:Vector<?>

the names of the columns

Annotations
@SuppressWarnings:rawtypes, unchecked
See Also
getDataVector
setDataVectorback to summary
public void setDataVector(Object[][] dataVector, Object[] columnIdentifiers)

Replaces the value in the 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:Object[][]

the new data vector

columnIdentifiers:Object[]

the names of the columns

See Also
setDataVector(Vector, Vector)
setNumRowsback to summary
public void setNumRows(int rowCount)

Obsolete as of Java 2 platform v1.3. Please use setRowCount instead.

Parameters
rowCount:int

the new number of rows

setRowCountback to summary
public void setRowCount(int rowCount)

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 rowCount and greater are discarded.

Parameters
rowCount:int

number of rows in the model

Since
1.3
See Also
setColumnCount
setValueAtback to summary
public void setValueAt(Object aValue, int row, int column)

Overrides javax.swing.table.AbstractTableModel.setValueAt.

Implements javax.swing.table.TableModel.setValueAt.

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:Object

the new value; this can be null

row:int

the row whose value is to be changed

column:int

the column whose value is to be changed

Exceptions
ArrayIndexOutOfBoundsException:
if an invalid row or column was given