- java.lang.Object
-
- javax.swing.table.AbstractTableModel
-
- javax.swing.table.DefaultTableModel
-
- All Implemented Interfaces:
Serializable
,TableModel
public class DefaultTableModel extends AbstractTableModel implements Serializable
This is an implementation ofTableModel
that uses aVector
ofVectors
to store the cell value objects.Warning:
DefaultTableModel
returns a column class ofObject
. WhenDefaultTableModel
is used with aTableRowSorter
this will result in extensive use oftoString
, which for non-String
data types is expensive. If you useDefaultTableModel
with aTableRowSorter
you are strongly encouraged to overridegetColumnClass
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 seeXMLEncoder
.- See Also:
TableModel
,getDataVector()
, Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description protected Vector
columnIdentifiers
TheVector
of column identifiers.protected Vector<Vector>
dataVector
TheVector
ofVectors
ofObject
values.-
Fields declared in class javax.swing.table.AbstractTableModel
listenerList
-
-
Constructor Summary
Constructors Constructor Description DefaultTableModel()
Constructs a defaultDefaultTableModel
which is a table of zero columns and zero rows.DefaultTableModel(int rowCount, int columnCount)
Constructs aDefaultTableModel
withrowCount
andcolumnCount
ofnull
object values.DefaultTableModel(Object[][] data, Object[] columnNames)
Constructs aDefaultTableModel
and initializes the table by passingdata
andcolumnNames
to thesetDataVector
method.DefaultTableModel(Object[] columnNames, int rowCount)
Constructs aDefaultTableModel
with as many columns as there are elements incolumnNames
androwCount
ofnull
object values.DefaultTableModel(Vector<?> columnNames, int rowCount)
Constructs aDefaultTableModel
with as many columns as there are elements incolumnNames
androwCount
ofnull
object values.DefaultTableModel(Vector<? extends Vector> data, Vector<?> columnNames)
Constructs aDefaultTableModel
and initializes the table by passingdata
andcolumnNames
to thesetDataVector
method.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addColumn(Object columnName)
Adds a column to the model.void
addColumn(Object columnName, Object[] columnData)
Adds a column to the model.void
addColumn(Object columnName, Vector<?> columnData)
Adds a column to the model.void
addRow(Object[] rowData)
Adds a row to the end of the model.void
addRow(Vector<?> rowData)
Adds a row to the end of the model.protected static Vector<Object>
convertToVector(Object[] anArray)
Returns a vector that contains the same objects as the array.protected static Vector<Vector<Object>>
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<Vector>
getDataVector()
Returns theVector
ofVectors
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 atrow
andcolumn
.void
insertRow(int row, Object[] rowData)
Inserts a row atrow
in the model.void
insertRow(int row, Vector<?> rowData)
Inserts a row atrow
in the model.boolean
isCellEditable(int row, int column)
Returns true regardless of parameter values.void
moveRow(int start, int end, int to)
Moves one or more rows from the inclusive rangestart
toend
to theto
position in the model.void
newDataAvailable(TableModelEvent event)
Equivalent tofireTableChanged
.void
newRowsAdded(TableModelEvent e)
Ensures that the new rows have the correct number of columns.void
removeRow(int row)
Removes the row atrow
from the model.void
rowsRemoved(TableModelEvent event)
Equivalent tofireTableChanged
.void
setColumnCount(int columnCount)
Sets the number of columns in the model.void
setColumnIdentifiers(Object[] newIdentifiers)
Replaces the column identifiers in the model.void
setColumnIdentifiers(Vector<?> columnIdentifiers)
Replaces the column identifiers in the model.void
setDataVector(Object[][] dataVector, Object[] columnIdentifiers)
Replaces the value in thedataVector
instance variable with the values in the arraydataVector
.void
setDataVector(Vector<? extends Vector> dataVector, Vector<?> columnIdentifiers)
Replaces the currentdataVector
instance variable with the newVector
of rows,dataVector
.void
setNumRows(int rowCount)
Obsolete as of Java 2 platform v1.3.void
setRowCount(int rowCount)
Sets the number of rows in the model.void
setValueAt(Object aValue, int row, int column)
Sets the object value for the cell atcolumn
androw
.-
Methods declared in class javax.swing.table.AbstractTableModel
addTableModelListener, findColumn, fireTableCellUpdated, fireTableChanged, fireTableDataChanged, fireTableRowsDeleted, fireTableRowsInserted, fireTableRowsUpdated, fireTableStructureChanged, getColumnClass, getListeners, getTableModelListeners, removeTableModelListener
-
-
-
-
Constructor Detail
-
DefaultTableModel
public DefaultTableModel()
Constructs a defaultDefaultTableModel
which is a table of zero columns and zero rows.
-
DefaultTableModel
public DefaultTableModel(int rowCount, int columnCount)
Constructs aDefaultTableModel
withrowCount
andcolumnCount
ofnull
object values.- Parameters:
rowCount
- the number of rows the table holdscolumnCount
- the number of columns the table holds- See Also:
setValueAt(java.lang.Object, int, int)
-
DefaultTableModel
public DefaultTableModel(Vector<?> columnNames, int rowCount)
Constructs aDefaultTableModel
with as many columns as there are elements incolumnNames
androwCount
ofnull
object values. Each column's name will be taken from thecolumnNames
vector.- Parameters:
columnNames
-vector
containing the names of the new columns; if this isnull
then the model has no columnsrowCount
- the number of rows the table holds- See Also:
setDataVector(java.util.Vector<? extends java.util.Vector>, java.util.Vector<?>)
,setValueAt(java.lang.Object, int, int)
-
DefaultTableModel
public DefaultTableModel(Object[] columnNames, int rowCount)
Constructs aDefaultTableModel
with as many columns as there are elements incolumnNames
androwCount
ofnull
object values. Each column's name will be taken from thecolumnNames
array.- Parameters:
columnNames
-array
containing the names of the new columns; if this isnull
then the model has no columnsrowCount
- the number of rows the table holds- See Also:
setDataVector(java.util.Vector<? extends java.util.Vector>, java.util.Vector<?>)
,setValueAt(java.lang.Object, int, int)
-
DefaultTableModel
public DefaultTableModel(Vector<? extends Vector> data, Vector<?> columnNames)
Constructs aDefaultTableModel
and initializes the table by passingdata
andcolumnNames
to thesetDataVector
method.- Parameters:
data
- the data of the table, aVector
ofVector
s ofObject
valuescolumnNames
-vector
containing the names of the new columns- See Also:
getDataVector()
,setDataVector(java.util.Vector<? extends java.util.Vector>, java.util.Vector<?>)
-
DefaultTableModel
public DefaultTableModel(Object[][] data, Object[] columnNames)
Constructs aDefaultTableModel
and initializes the table by passingdata
andcolumnNames
to thesetDataVector
method. The first index in theObject[][]
array is the row index and the second is the column index.- Parameters:
data
- the data of the tablecolumnNames
- the names of the columns- See Also:
getDataVector()
,setDataVector(java.util.Vector<? extends java.util.Vector>, java.util.Vector<?>)
-
-
Method Detail
-
getDataVector
public Vector<Vector> getDataVector()
Returns theVector
ofVectors
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:
newDataAvailable(javax.swing.event.TableModelEvent)
,newRowsAdded(javax.swing.event.TableModelEvent)
,setDataVector(java.util.Vector<? extends java.util.Vector>, java.util.Vector<?>)
-
setDataVector
public void setDataVector(Vector<? extends Vector> dataVector, Vector<?> columnIdentifiers)
Replaces the currentdataVector
instance variable with the newVector
of rows,dataVector
. Each row is represented indataVector
as aVector
ofObject
values.columnIdentifiers
are the names of the new columns. The first name incolumnIdentifiers
is mapped to column 0 indataVector
. Each row indataVector
is adjusted to match the number of columns incolumnIdentifiers
either by truncating theVector
if it is too long, or addingnull
values if it is too short.Note that passing in a
null
value fordataVector
results in unspecified behavior, an possibly an exception.- Parameters:
dataVector
- the new data vectorcolumnIdentifiers
- the names of the columns- See Also:
getDataVector()
-
setDataVector
public void setDataVector(Object[][] dataVector, Object[] columnIdentifiers)
Replaces the value in thedataVector
instance variable with the values in the arraydataVector
. The first index in theObject[][]
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 vectorcolumnIdentifiers
- the names of the columns- See Also:
setDataVector(Vector, Vector)
-
newDataAvailable
public void newDataAvailable(TableModelEvent event)
Equivalent tofireTableChanged
.- Parameters:
event
- the change event
-
newRowsAdded
public void newRowsAdded(TableModelEvent e)
Ensures that the new rows have the correct number of columns. This is accomplished by using thesetSize
method inVector
which truncates vectors which are too long, and appendsnull
s if they are too short. This method also sends out atableChanged
notification message to all the listeners.- Parameters:
e
- thisTableModelEvent
describes where the rows were added. Ifnull
it assumes all the rows were newly added- See Also:
getDataVector()
-
rowsRemoved
public void rowsRemoved(TableModelEvent event)
Equivalent tofireTableChanged
.- Parameters:
event
- the change event
-
setNumRows
public void setNumRows(int rowCount)
Obsolete as of Java 2 platform v1.3. Please usesetRowCount
instead.- Parameters:
rowCount
- the new number of rows
-
setRowCount
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 indexrowCount
and greater are discarded.- Parameters:
rowCount
- number of rows in the model- Since:
- 1.3
- See Also:
setColumnCount(int)
-
addRow
public void addRow(Vector<?> rowData)
Adds a row to the end of the model. The new row will containnull
values unlessrowData
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)
Adds a row to the end of the model. The new row will containnull
values unlessrowData
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)
Inserts a row atrow
in the model. The new row will containnull
values unlessrowData
is specified. Notification of the row being added will be generated.- Parameters:
row
- the row index of the row to be insertedrowData
- optional data of the row being added- Throws:
ArrayIndexOutOfBoundsException
- if the row was invalid
-
insertRow
public void insertRow(int row, Object[] rowData)
Inserts a row atrow
in the model. The new row will containnull
values unlessrowData
is specified. Notification of the row being added will be generated.- Parameters:
row
- the row index of the row to be insertedrowData
- optional data of the row being added- Throws:
ArrayIndexOutOfBoundsException
- if the row was invalid
-
moveRow
public void moveRow(int start, int end, int to)
Moves one or more rows from the inclusive rangestart
toend
to theto
position in the model. After the move, the row that was at indexstart
will be at indexto
. This method will send atableChanged
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 movedend
- the ending row index to be movedto
- 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)
Removes the row atrow
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)
Replaces the column identifiers in the model. If the number ofnewIdentifier
s is greater than the current number of columns, new columns are added to the end of each row in the model. If the number ofnewIdentifier
s 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:
setNumRows(int)
-
setColumnIdentifiers
public void setColumnIdentifiers(Object[] newIdentifiers)
Replaces the column identifiers in the model. If the number ofnewIdentifier
s is greater than the current number of columns, new columns are added to the end of each row in the model. If the number ofnewIdentifier
s 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:
setNumRows(int)
-
setColumnCount
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 withnull
cell values. If the new size is less than the current size, all columns at indexcolumnCount
and greater are discarded.- Parameters:
columnCount
- the new number of columns in the model- Since:
- 1.3
- See Also:
setColumnCount(int)
-
addColumn
public void addColumn(Object columnName)
Adds a column to the model. The new column will have the identifiercolumnName
, which may be null. This method will send atableChanged
notification message to all the listeners. This method is a cover foraddColumn(Object, Vector)
which usesnull
as the data vector.- Parameters:
columnName
- the identifier of the column being added
-
addColumn
public void addColumn(Object columnName, Vector<?> columnData)
Adds a column to the model. The new column will have the identifiercolumnName
, which may be null.columnData
is the optional vector of data for the column. If it isnull
the column is filled withnull
values. Otherwise, the new data will be added to model starting with the first element going to row 0, etc. This method will send atableChanged
notification message to all the listeners.- Parameters:
columnName
- the identifier of the column being addedcolumnData
- optional data of the column being added
-
addColumn
public void addColumn(Object columnName, Object[] columnData)
Adds a column to the model. The new column will have the identifiercolumnName
.columnData
is the optional array of data for the column. If it isnull
the column is filled withnull
values. Otherwise, the new data will be added to model starting with the first element going to row 0, etc. This method will send atableChanged
notification message to all the listeners.- Parameters:
columnName
- identifier of the newly created columncolumnData
- new data to be added to the column- See Also:
addColumn(Object, Vector)
-
getRowCount
public int getRowCount()
Returns the number of rows in this data table.- Specified by:
getRowCount
in interfaceTableModel
- Returns:
- the number of rows in the model
- See Also:
TableModel.getColumnCount()
-
getColumnCount
public int getColumnCount()
Returns the number of columns in this data table.- Specified by:
getColumnCount
in interfaceTableModel
- Returns:
- the number of columns in the model
- See Also:
TableModel.getRowCount()
-
getColumnName
public String getColumnName(int column)
Returns the column name.- Specified by:
getColumnName
in interfaceTableModel
- Overrides:
getColumnName
in classAbstractTableModel
- Parameters:
column
- the column being queried- Returns:
- a name for this column using the string value of the
appropriate member in
columnIdentifiers
. IfcolumnIdentifiers
does not have an entry for this index, returns the default name provided by the superclass.
-
isCellEditable
public boolean isCellEditable(int row, int column)
Returns true regardless of parameter values.- Specified by:
isCellEditable
in interfaceTableModel
- Overrides:
isCellEditable
in classAbstractTableModel
- Parameters:
row
- the row whose value is to be queriedcolumn
- the column whose value is to be queried- Returns:
- true
- See Also:
setValueAt(java.lang.Object, int, int)
-
getValueAt
public Object getValueAt(int row, int column)
Returns an attribute value for the cell atrow
andcolumn
.- Specified by:
getValueAt
in interfaceTableModel
- Parameters:
row
- the row whose value is to be queriedcolumn
- 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)
Sets the object value for the cell atcolumn
androw
.aValue
is the new value. This method will generate atableChanged
notification.- Specified by:
setValueAt
in interfaceTableModel
- Overrides:
setValueAt
in classAbstractTableModel
- Parameters:
aValue
- the new value; this can be nullrow
- the row whose value is to be changedcolumn
- the column whose value is to be changed- Throws:
ArrayIndexOutOfBoundsException
- if an invalid row or column was given- See Also:
TableModel.getValueAt(int, int)
,TableModel.isCellEditable(int, int)
-
convertToVector
protected static Vector<Object> convertToVector(Object[] anArray)
Returns a vector that contains the same objects as the array.- Parameters:
anArray
- the array to be converted- Returns:
- the new vector; if
anArray
isnull
, returnsnull
-
-