- 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 ofTableModelthat uses aVectorofVectorsto store the cell value objects.Warning: DefaultTableModelreturns a column class ofObject. WhenDefaultTableModelis used with aTableRowSorterthis will result in extensive use oftoString, which for non-Stringdata types is expensive. If you useDefaultTableModelwith aTableRowSorteryou are strongly encouraged to overridegetColumnClassto 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.beanspackage. Please seeXMLEncoder.- See Also:
- TableModel,- getDataVector(), Serialized Form
 
- 
- 
Field SummaryFields Modifier and Type Field Description protected VectorcolumnIdentifiersTheVectorof column identifiers.protected Vector<Vector>dataVectorTheVectorofVectorsofObjectvalues.- 
Fields declared in class javax.swing.table.AbstractTableModellistenerList
 
- 
 - 
Constructor SummaryConstructors Constructor Description DefaultTableModel()Constructs 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 SummaryAll Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddColumn(Object columnName)Adds a column to the model.voidaddColumn(Object columnName, Object[] columnData)Adds a column to the model.voidaddColumn(Object columnName, Vector<?> columnData)Adds a column to the model.voidaddRow(Object[] rowData)Adds a row to the end of the model.voidaddRow(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.intgetColumnCount()Returns the number of columns in this data table.StringgetColumnName(int column)Returns the column name.Vector<Vector>getDataVector()Returns theVectorofVectorsthat contains the table's data values.intgetRowCount()Returns the number of rows in this data table.ObjectgetValueAt(int row, int column)Returns an attribute value for the cell atrowandcolumn.voidinsertRow(int row, Object[] rowData)Inserts a row atrowin the model.voidinsertRow(int row, Vector<?> rowData)Inserts 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.voidnewRowsAdded(TableModelEvent e)Ensures 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.AbstractTableModeladdTableModelListener, findColumn, fireTableCellUpdated, fireTableChanged, fireTableDataChanged, fireTableRowsDeleted, fireTableRowsInserted, fireTableRowsUpdated, fireTableStructureChanged, getColumnClass, getListeners, getTableModelListeners, removeTableModelListener
 
- 
 
- 
- 
- 
Constructor Detail- 
DefaultTableModelpublic DefaultTableModel() Constructs a defaultDefaultTableModelwhich is a table of zero columns and zero rows.
 - 
DefaultTableModelpublic DefaultTableModel(int rowCount, int columnCount)Constructs aDefaultTableModelwithrowCountandcolumnCountofnullobject values.- Parameters:
- rowCount- the number of rows the table holds
- columnCount- the number of columns the table holds
- See Also:
- setValueAt(java.lang.Object, int, int)
 
 - 
DefaultTableModelpublic DefaultTableModel(Vector<?> columnNames, int rowCount) 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 is- nullthen the model has no columns
- rowCount- 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)
 
 - 
DefaultTableModelpublic DefaultTableModel(Object[] columnNames, int rowCount) 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 is- nullthen the model has no columns
- rowCount- 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)
 
 - 
DefaultTableModelpublic DefaultTableModel(Vector<? extends Vector> data, Vector<?> columnNames) Constructs aDefaultTableModeland initializes the table by passingdataandcolumnNamesto thesetDataVectormethod.- Parameters:
- data- the data of the table, a- Vectorof- Vectors of- Objectvalues
- columnNames-- vectorcontaining the names of the new columns
- See Also:
- getDataVector(),- setDataVector(java.util.Vector<? extends java.util.Vector>, java.util.Vector<?>)
 
 - 
DefaultTableModelpublic DefaultTableModel(Object[][] data, Object[] columnNames) 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 table
- columnNames- the names of the columns
- See Also:
- getDataVector(),- setDataVector(java.util.Vector<? extends java.util.Vector>, java.util.Vector<?>)
 
 
- 
 - 
Method Detail- 
getDataVectorpublic Vector<Vector> 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:
- newDataAvailable(javax.swing.event.TableModelEvent),- newRowsAdded(javax.swing.event.TableModelEvent),- setDataVector(java.util.Vector<? extends java.util.Vector>, java.util.Vector<?>)
 
 - 
setDataVectorpublic void setDataVector(Vector<? extends Vector> dataVector, Vector<?> columnIdentifiers) 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 vector
- columnIdentifiers- the names of the columns
- See Also:
- getDataVector()
 
 - 
setDataVectorpublic void setDataVector(Object[][] dataVector, Object[] columnIdentifiers) 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 vector
- columnIdentifiers- the names of the columns
- See Also:
- setDataVector(Vector, Vector)
 
 - 
newDataAvailablepublic void newDataAvailable(TableModelEvent event) Equivalent tofireTableChanged.- Parameters:
- event- the change event
 
 - 
newRowsAddedpublic void newRowsAdded(TableModelEvent e) 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- this- TableModelEventdescribes where the rows were added. If- nullit assumes all the rows were newly added
- See Also:
- getDataVector()
 
 - 
rowsRemovedpublic void rowsRemoved(TableModelEvent event) Equivalent tofireTableChanged.- Parameters:
- event- the change event
 
 - 
setNumRowspublic void setNumRows(int rowCount) Obsolete as of Java 2 platform v1.3. Please usesetRowCountinstead.- Parameters:
- rowCount- the new number of rows
 
 - 
setRowCountpublic 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:
- setColumnCount(int)
 
 - 
addRowpublic void addRow(Vector<?> rowData) 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
 
 - 
addRowpublic void addRow(Object[] rowData) 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
 
 - 
insertRowpublic void insertRow(int row, Vector<?> rowData)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 inserted
- rowData- optional data of the row being added
- Throws:
- ArrayIndexOutOfBoundsException- if the row was invalid
 
 - 
insertRowpublic void insertRow(int row, Object[] rowData)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 inserted
- rowData- optional data of the row being added
- Throws:
- ArrayIndexOutOfBoundsException- if the row was invalid
 
 - 
moveRowpublic 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 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
 
 - 
removeRowpublic 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
 
 - 
setColumnIdentifierspublic void setColumnIdentifiers(Vector<?> columnIdentifiers) 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. If- null, set the model to zero columns
- See Also:
- setNumRows(int)
 
 - 
setColumnIdentifierspublic void setColumnIdentifiers(Object[] newIdentifiers) 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. If- null, set the model to zero columns
- See Also:
- setNumRows(int)
 
 - 
setColumnCountpublic 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:
- setColumnCount(int)
 
 - 
addColumnpublic 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 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
 
 - 
addColumnpublic void addColumn(Object columnName, Vector<?> columnData) 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 added
- columnData- optional data of the column being added
 
 - 
addColumnpublic void addColumn(Object columnName, Object[] columnData) 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 column
- columnData- new data to be added to the column
- See Also:
- addColumn(Object, Vector)
 
 - 
getRowCountpublic int getRowCount() Returns the number of rows in this data table.- Specified by:
- getRowCountin interface- TableModel
- Returns:
- the number of rows in the model
- See Also:
- TableModel.getColumnCount()
 
 - 
getColumnCountpublic int getColumnCount() Returns the number of columns in this data table.- Specified by:
- getColumnCountin interface- TableModel
- Returns:
- the number of columns in the model
- See Also:
- TableModel.getRowCount()
 
 - 
getColumnNamepublic String getColumnName(int column) Returns the column name.- Specified by:
- getColumnNamein interface- TableModel
- Overrides:
- getColumnNamein class- AbstractTableModel
- 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.
 
 - 
isCellEditablepublic boolean isCellEditable(int row, int column)Returns true regardless of parameter values.- Specified by:
- isCellEditablein interface- TableModel
- Overrides:
- isCellEditablein class- AbstractTableModel
- Parameters:
- row- the row whose value is to be queried
- column- the column whose value is to be queried
- Returns:
- true
- See Also:
- setValueAt(java.lang.Object, int, int)
 
 - 
getValueAtpublic Object getValueAt(int row, int column) Returns an attribute value for the cell atrowandcolumn.- Specified by:
- getValueAtin interface- TableModel
- 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
 
 - 
setValueAtpublic void setValueAt(Object aValue, int row, int column) Sets the object value for the cell atcolumnandrow.aValueis the new value. This method will generate atableChangednotification.- Specified by:
- setValueAtin interface- TableModel
- Overrides:
- setValueAtin class- AbstractTableModel
- 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:
- TableModel.getValueAt(int, int),- TableModel.isCellEditable(int, int)
 
 - 
convertToVectorprotected 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 anArrayisnull, returnsnull
 
 
- 
 
-