public class DefaultTableModel extends AbstractTableModelimplements Serializable
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.
TableModel,
getDataVector()
| Modifier and Type | Field and Description |
|---|---|
protected Vector |
columnIdentifiers
The
Vector of column identifiers.
|
protected Vector |
dataVector
The
Vector of
Vectors of
Object values.
|
listenerList| Constructor and Description |
|---|
DefaultTableModel()
Constructs a default
DefaultTableModel which is a table of zero columns and zero rows.
|
DefaultTableModel(int rowCount, int columnCount)
Constructs a
DefaultTableModel with
rowCount and
columnCount of
null object values.
|
DefaultTableModel(Object
Constructs a
DefaultTableModel and initializes the table by passing
data and
columnNames to the
setDataVector method.
|
DefaultTableModel(Object
Constructs a
DefaultTableModel with as many columns as there are elements in
columnNames and
rowCount of
null object values.
|
DefaultTableModel(Vector
Constructs a
DefaultTableModel with as many columns as there are elements in
columnNames and
rowCount of
null object values.
|
DefaultTableModel(Vector
Constructs a
DefaultTableModel and initializes the table by passing
data and
columnNames to the
setDataVector method.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addColumn(Object
Adds a column to the model.
|
void |
addColumn(Object
Adds a column to the model.
|
void |
addColumn(Object
Adds a column to the model.
|
void |
addRow(Object
Adds a row to the end of the model.
|
void |
addRow(Vector
Adds a row to the end of the model.
|
protected static Vector |
convertToVector(Object
Returns a vector that contains the same objects as the array.
|
protected static Vector |
convertToVector(Object
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 |
getDataVector()
Returns the
Vector of
Vectors 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 at
row and
column.
|
void |
insertRow(int row, Object
Inserts a row at
row in the model.
|
void |
insertRow(int row, Vector
Inserts a row at
row 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 range
start to
end to the
to position in the model.
|
void |
newDataAvailable(TableModelEvent
Equivalent to
fireTableChanged.
|
void |
newRowsAdded(TableModelEvent
Ensures that the new rows have the correct number of columns.
|
void |
removeRow(int row)
Removes the row at
row from the model.
|
void |
rowsRemoved(TableModelEvent
Equivalent to
fireTableChanged.
|
void |
setColumnCount(int columnCount)
Sets the number of columns in the model.
|
void |
setColumnIdentifiers(Object
Replaces the column identifiers in the model.
|
void |
setColumnIdentifiers(Vector
Replaces the column identifiers in the model.
|
void |
setDataVector(Object
Replaces the value in the
dataVector instance variable with the values in the array
dataVector.
|
void |
setDataVector(Vector
Replaces the current
dataVector instance variable with the new
Vector 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
Sets the object value for the cell at
column and
row.
|
addTableModelListener, findColumn, fireTableCellUpdated, fireTableChanged, fireTableDataChanged, fireTableRowsDeleted, fireTableRowsInserted, fireTableRowsUpdated, fireTableStructureChanged, getColumnClass, getListeners, getTableModelListeners, removeTableModelListenerprotected VectordataVector
Vector of
Vectors of
Object values.
protected VectorcolumnIdentifiers
Vector of column identifiers.
public DefaultTableModel()
DefaultTableModel which is a table of zero columns and zero rows.
public DefaultTableModel(int rowCount,
int columnCount)
DefaultTableModel with
rowCount and
columnCount of
null object values.
rowCount - the number of rows the table holds
columnCount - the number of columns the table holds
setValueAt(java.lang.Object, int, int)
public DefaultTableModel(VectorcolumnNames, int rowCount)
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.
columnNames -
vector containing the names of the new columns; if this is
null then the model has no columns
rowCount - the number of rows the table holds
setDataVector(java.util.Vector, java.util.Vector),
setValueAt(java.lang.Object, int, int)
public DefaultTableModel(Object[] columnNames, int rowCount)
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.
columnNames -
array containing the names of the new columns; if this is
null then the model has no columns
rowCount - the number of rows the table holds
setDataVector(java.util.Vector, java.util.Vector),
setValueAt(java.lang.Object, int, int)
public DefaultTableModel(Vectordata, Vector columnNames)
DefaultTableModel and initializes the table by passing
data and
columnNames to the
setDataVector method.
data - the data of the table, a
Vector of
Vectors of
Object values
columnNames -
vector containing the names of the new columns
getDataVector(),
setDataVector(java.util.Vector, java.util.Vector)
public DefaultTableModel(Object[][] data, Object [] columnNames)
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.
data - the data of the table
columnNames - the names of the columns
getDataVector(),
setDataVector(java.util.Vector, java.util.Vector)
public VectorgetDataVector()
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);
newDataAvailable(javax.swing.event.TableModelEvent),
newRowsAdded(javax.swing.event.TableModelEvent),
setDataVector(java.util.Vector, java.util.Vector)
public void setDataVector(VectordataVector, Vector columnIdentifiers)
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.
dataVector - the new data vector
columnIdentifiers - the names of the columns
getDataVector()
public void setDataVector(Object[][] dataVector, Object [] columnIdentifiers)
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.
dataVector - the new data vector
columnIdentifiers - the names of the columns
setDataVector(Vector, Vector)
public void newDataAvailable(TableModelEventevent)
fireTableChanged.
event - the change event
public void newRowsAdded(TableModelEvente)
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.
e - this
TableModelEvent describes where the rows were added. If
null it assumes all the rows were newly added
getDataVector()
public void rowsRemoved(TableModelEventevent)
fireTableChanged.
event - the change event
public void setNumRows(int rowCount)
setRowCount instead.
public void setRowCount(int rowCount)
rowCount and greater are discarded.
setColumnCount(int)
public void addRow(VectorrowData)
null values unless
rowData is specified. Notification of the row being added will be generated.
rowData - optional data of the row being added
public void addRow(Object[] rowData)
null values unless
rowData is specified. Notification of the row being added will be generated.
rowData - optional data of the row being added
public void insertRow(int row,
Vector rowData)
row in the model. The new row will contain
null values unless
rowData is specified. Notification of the row being added will be generated.
row - the row index of the row to be inserted
rowData - optional data of the row being added
ArrayIndexOutOfBoundsException - if the row was invalid
public void insertRow(int row,
Object[] rowData)
row in the model. The new row will contain
null values unless
rowData is specified. Notification of the row being added will be generated.
row - the row index of the row to be inserted
rowData - optional data of the row being added
ArrayIndexOutOfBoundsException - if the row was invalid
public void moveRow(int start,
int end,
int to)
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
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
ArrayIndexOutOfBoundsException - if any of the elements would be moved out of the table's range
public void removeRow(int row)
row from the model. Notification of the row being removed will be sent to all the listeners.
row - the row index of the row to be removed
ArrayIndexOutOfBoundsException - if the row was invalid
public void setColumnIdentifiers(VectorcolumnIdentifiers)
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.
columnIdentifiers - vector of column identifiers. If
null, set the model to zero columns
setNumRows(int)
public void setColumnIdentifiers(Object[] newIdentifiers)
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.
newIdentifiers - array of column identifiers. If
null, set the model to zero columns
setNumRows(int)
public void setColumnCount(int columnCount)
null cell values. If the new size is less than the current size, all columns at index
columnCount and greater are discarded.
columnCount - the new number of columns in the model
setColumnCount(int)
public void addColumn(ObjectcolumnName)
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.
columnName - the identifier of the column being added
public void addColumn(ObjectcolumnName, Vector columnData)
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.
columnName - the identifier of the column being added
columnData - optional data of the column being added
public void addColumn(ObjectcolumnName, Object [] columnData)
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.
addColumn(Object, Vector)
public int getRowCount()
getRowCount in interface
TableModel
TableModel.getColumnCount()
public int getColumnCount()
getColumnCount in interface
TableModel
TableModel.getRowCount()
public StringgetColumnName(int column)
getColumnName in interface
TableModel
getColumnName in class
AbstractTableModel
column - the column being queried
columnIdentifiers. If
columnIdentifiers does not have an entry for this index, returns the default name provided by the superclass.
public boolean isCellEditable(int row,
int column)
isCellEditable in interface
TableModel
isCellEditable in class
AbstractTableModel
row - the row whose value is to be queried
column - the column whose value is to be queried
setValueAt(java.lang.Object, int, int)
public ObjectgetValueAt(int row, int column)
row and
column.
getValueAt in interface
TableModel
row - the row whose value is to be queried
column - the column whose value is to be queried
ArrayIndexOutOfBoundsException - if an invalid row or column was given
public void setValueAt(ObjectaValue, int row, int column)
column and
row.
aValue is the new value. This method will generate a
tableChanged notification.
setValueAt in interface
TableModel
setValueAt in class
AbstractTableModel
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
ArrayIndexOutOfBoundsException - if an invalid row or column was given
TableModel.getValueAt(int, int) ,
TableModel.isCellEditable(int, int)
protected static VectorconvertToVector(Object [] anArray)
anArray - the array to be converted
anArray is
null, returns
null