- All Implemented Interfaces:
Serializable,TableModel
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
FieldsModifier and TypeFieldDescriptionprotected VectorTheVectorof column identifiers.TheVectorofVectorsofObjectvalues.Fields declared in class javax.swing.table.AbstractTableModel
listenerList -
Constructor Summary
ConstructorsConstructorDescriptionConstructs a defaultDefaultTableModelwhich is a table of zero columns and zero rows.DefaultTableModel(int rowCount, int columnCount) Constructs aDefaultTableModelwithrowCountandcolumnCountofnullobject values.DefaultTableModel(Object[][] data, Object[] columnNames) Constructs aDefaultTableModeland initializes the table by passingdataandcolumnNamesto thesetDataVectormethod.DefaultTableModel(Object[] columnNames, int rowCount) Constructs aDefaultTableModelwith as many columns as there are elements incolumnNamesandrowCountofnullobject values.DefaultTableModel(Vector<?> columnNames, int rowCount) Constructs aDefaultTableModelwith as many columns as there are elements incolumnNamesandrowCountofnullobject values.DefaultTableModel(Vector<? extends Vector> data, Vector<?> columnNames) Constructs aDefaultTableModeland initializes the table by passingdataandcolumnNamesto thesetDataVectormethod. -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds a column to the model.voidAdds a column to the model.voidAdds a column to the model.voidAdds a row to the end of the model.voidAdds a row to the end of the model.convertToVector(Object[] anArray) Returns a vector that contains the same objects as the array.convertToVector(Object[][] anArray) Returns a vector of vectors that contains the same objects as the array.intReturns the number of columns in this data table.getColumnName(int column) Returns the column name.Returns theVectorofVectorsthat contains the table's data values.intReturns the number of rows in this data table.getValueAt(int row, int column) Returns an attribute value for the cell atrowandcolumn.voidInserts a row atrowin the model.voidInserts a row atrowin the model.booleanisCellEditable(int row, int column) Returns true regardless of parameter values.voidmoveRow(int start, int end, int to) Moves one or more rows from the inclusive rangestarttoendto thetoposition in the model.voidnewDataAvailable(TableModelEvent event) Equivalent tofireTableChanged.voidEnsures that the new rows have the correct number of columns.voidremoveRow(int row) Removes the row atrowfrom the model.voidrowsRemoved(TableModelEvent event) Equivalent tofireTableChanged.voidsetColumnCount(int columnCount) Sets the number of columns in the model.voidsetColumnIdentifiers(Object[] newIdentifiers) Replaces the column identifiers in the model.voidsetColumnIdentifiers(Vector<?> columnIdentifiers) Replaces the column identifiers in the model.voidsetDataVector(Object[][] dataVector, Object[] columnIdentifiers) Replaces the value in thedataVectorinstance variable with the values in the arraydataVector.voidsetDataVector(Vector<? extends Vector> dataVector, Vector<?> columnIdentifiers) Replaces the currentdataVectorinstance variable with the newVectorof rows,dataVector.voidsetNumRows(int rowCount) Obsolete as of Java 2 platform v1.3.voidsetRowCount(int rowCount) Sets the number of rows in the model.voidsetValueAt(Object aValue, int row, int column) Sets the object value for the cell atcolumnandrow.Methods declared in class javax.swing.table.AbstractTableModel
addTableModelListener, findColumn, fireTableCellUpdated, fireTableChanged, fireTableDataChanged, fireTableRowsDeleted, fireTableRowsInserted, fireTableRowsUpdated, fireTableStructureChanged, getColumnClass, getListeners, getTableModelListeners, removeTableModelListener
-
Field Details
-
dataVector
TheVectorofVectorsofObjectvalues. -
columnIdentifiers
TheVectorof column identifiers.
-
-
Constructor Details
-
DefaultTableModel
public DefaultTableModel()Constructs a defaultDefaultTableModelwhich is a table of zero columns and zero rows. -
DefaultTableModel
public DefaultTableModel(int rowCount, int columnCount) Constructs aDefaultTableModelwithrowCountandcolumnCountofnullobject values.- Parameters:
rowCount- the number of rows the table holdscolumnCount- the number of columns the table holds- See Also:
-
DefaultTableModel
Constructs aDefaultTableModelwith as many columns as there are elements incolumnNamesandrowCountofnullobject values. Each column's name will be taken from thecolumnNamesvector.- Parameters:
columnNames-vectorcontaining the names of the new columns; if this isnullthen the model has no columnsrowCount- the number of rows the table holds- See Also:
-
DefaultTableModel
Constructs aDefaultTableModelwith as many columns as there are elements incolumnNamesandrowCountofnullobject values. Each column's name will be taken from thecolumnNamesarray.- Parameters:
columnNames-arraycontaining the names of the new columns; if this isnullthen the model has no columnsrowCount- the number of rows the table holds- See Also:
-
DefaultTableModel
Constructs aDefaultTableModeland initializes the table by passingdataandcolumnNamesto thesetDataVectormethod.- Parameters:
data- the data of the table, aVectorofVectors ofObjectvaluescolumnNames-vectorcontaining the names of the new columns- See Also:
-
DefaultTableModel
Constructs aDefaultTableModeland initializes the table by passingdataandcolumnNamesto thesetDataVectormethod. 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:
-
-
Method Details
-
getDataVector
Returns theVectorofVectorsthat 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
Replaces the currentdataVectorinstance variable with the newVectorof rows,dataVector. Each row is represented indataVectoras aVectorofObjectvalues.columnIdentifiersare the names of the new columns. The first name incolumnIdentifiersis mapped to column 0 indataVector. Each row indataVectoris adjusted to match the number of columns incolumnIdentifierseither by truncating theVectorif it is too long, or addingnullvalues if it is too short.Note that passing in a
nullvalue fordataVectorresults in unspecified behavior, an possibly an exception.- Parameters:
dataVector- the new data vectorcolumnIdentifiers- the names of the columns- See Also:
-
setDataVector
Replaces the value in thedataVectorinstance variable with the values in the arraydataVector. The first index in theObject[][]array is the row index and the second is the column index.columnIdentifiersare the names of the new columns.- Parameters:
dataVector- the new data vectorcolumnIdentifiers- the names of the columns- See Also:
-
newDataAvailable
Equivalent tofireTableChanged.- Parameters:
event- the change event
-
newRowsAdded
Ensures that the new rows have the correct number of columns. This is accomplished by using thesetSizemethod inVectorwhich truncates vectors which are too long, and appendsnulls if they are too short. This method also sends out atableChangednotification message to all the listeners.- Parameters:
e- thisTableModelEventdescribes where the rows were added. Ifnullit assumes all the rows were newly added- See Also:
-
rowsRemoved
Equivalent tofireTableChanged.- Parameters:
event- the change event
-
setNumRows
public void setNumRows(int rowCount) Obsolete as of Java 2 platform v1.3. Please usesetRowCountinstead.- 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 indexrowCountand greater are discarded.- Parameters:
rowCount- number of rows in the model- Since:
- 1.3
- See Also:
-
addRow
Adds a row to the end of the model. The new row will containnullvalues unlessrowDatais specified. Notification of the row being added will be generated.- Parameters:
rowData- optional data of the row being added
-
addRow
Adds a row to the end of the model. The new row will containnullvalues unlessrowDatais specified. Notification of the row being added will be generated.- Parameters:
rowData- optional data of the row being added
-
insertRow
Inserts a row atrowin the model. The new row will containnullvalues unlessrowDatais 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
Inserts a row atrowin the model. The new row will containnullvalues unlessrowDatais 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 rangestarttoendto thetoposition in the model. After the move, the row that was at indexstartwill be at indexto. This method will send atableChangednotification 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 atrowfrom 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
Replaces the column identifiers in the model. If the number ofnewIdentifiers is greater than the current number of columns, new columns are added to the end of each row in the model. If the number ofnewIdentifiers 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
Replaces the column identifiers in the model. If the number ofnewIdentifiers is greater than the current number of columns, new columns are added to the end of each row in the model. If the number ofnewIdentifiers 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) 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 withnullcell values. If the new size is less than the current size, all columns at indexcolumnCountand greater are discarded.- Parameters:
columnCount- the new number of columns in the model- Since:
- 1.3
- See Also:
-
addColumn
Adds a column to the model. The new column will have the identifiercolumnName, which may be null. This method will send atableChangednotification message to all the listeners. This method is a cover foraddColumn(Object, Vector)which usesnullas the data vector.- Parameters:
columnName- the identifier of the column being added
-
addColumn
Adds a column to the model. The new column will have the identifiercolumnName, which may be null.columnDatais the optional vector of data for the column. If it isnullthe column is filled withnullvalues. Otherwise, the new data will be added to model starting with the first element going to row 0, etc. This method will send atableChangednotification message to all the listeners.- Parameters:
columnName- the identifier of the column being addedcolumnData- optional data of the column being added
-
addColumn
Adds a column to the model. The new column will have the identifiercolumnName.columnDatais the optional array of data for the column. If it isnullthe column is filled withnullvalues. Otherwise, the new data will be added to model starting with the first element going to row 0, etc. This method will send atableChangednotification message to all the listeners.- Parameters:
columnName- identifier of the newly created columncolumnData- new data to be added to the column- See Also:
-
getRowCount
public int getRowCount()Returns the number of rows in this data table.- Specified by:
getRowCountin interfaceTableModel- Returns:
- the number of rows in the model
- See Also:
-
getColumnCount
public int getColumnCount()Returns the number of columns in this data table.- Specified by:
getColumnCountin interfaceTableModel- Returns:
- the number of columns in the model
- See Also:
-
getColumnName
Returns the column name.- 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) Returns true regardless of parameter values.- Specified by:
isCellEditablein interfaceTableModel- Overrides:
isCellEditablein classAbstractTableModel- Parameters:
row- the row whose value is to be queriedcolumn- the column whose value is to be queried- Returns:
- true
- See Also:
-
getValueAt
Returns an attribute value for the cell atrowandcolumn.- Specified by:
getValueAtin 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
Sets the object value for the cell atcolumnandrow.aValueis the new value. This method will generate atableChangednotification.- Specified by:
setValueAtin interfaceTableModel- Overrides:
setValueAtin 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:
-
convertToVector
Returns a vector that contains the same objects as the array.- Parameters:
anArray- the array to be converted- Returns:
- the new vector; if
anArrayisnull, returnsnull
-
convertToVector
Returns a vector of vectors that contains the same objects as the array.- Parameters:
anArray- the double array to be converted- Returns:
- the new vector of vectors; if
anArrayisnull, returnsnull
-