All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class com.sun.java.swing.table.DefaultTableModel

java.lang.Object
   |
   +----com.sun.java.swing.table.AbstractTableModel
           |
           +----com.sun.java.swing.table.DefaultTableModel

public class DefaultTableModel
extends AbstractTableModel
implements Serializable
This is an implementation of TableModel that uses a Vector of Vectors to store the cell value objects. The benefits of this implementation is that it is very simple to use, specially when it is coupled with JSimpleTable. Users is relieved of the task of writing a TableModel, instead users can think only in terms of a grid of values. In addition, because the model knows all the values it can provide sorting support.

Note: The DefaultTableModel's API contains the methods addColumn(), removeColumn(), but not methods to insert a column at an index nor methods to move the columns. This is because JTable does not display the columns based on the order of the columns in this model. So rearranging them here doesn't do much. See the column ordering methods in TableColumnModel. Or use JSimpleTable for a simpler API of JTable.

See Also:
TableModel, getDataVector

Variable Index

 o columnIdentifiers
The Vector column identifiers
 o dataVector
The Vector of Vector of Object values
 o EQUAL
 o GREATER_THAN
 o LESS_THAN

Constructor Index

 o DefaultTableModel()
Constructs a default DefaultTableModel which is a table of zero columns and zero rows.
 o DefaultTableModel(int, int)
Constructs a DefaultTableModel with numColumns and numRows of null object values.
 o DefaultTableModel(Object[], int)
Constructs a DefaultTableModel with as many columns as there are elements in columnIds and numRows of null object values.
 o DefaultTableModel(Object[][], Object[])
Constructs a DefaultTableModel and initialize the table by passing data and columnIds to the setDataVector() method.
 o DefaultTableModel(Vector, int)
Constructs a DefaultTableModel with as many columns as there are elements in columnIds and numRows of null object values.
 o DefaultTableModel(Vector, Vector)
Constructs a DefaultTableModel and initialize the table by passing data and columnIds to the setDataVector() method.

Method Index

 o addColumn(Object, Object[])
Add a column to the model.
 o addColumn(Object, Vector)
Add a column to the model.
 o addRow(Object[])
Add a row to the end of the model.
 o addRow(Vector)
Add a row to the end of the model.
 o changeColumnIdentifier(int, Object)
Replaces the identifier of column at columnIndex with newIdentifier.
 o changeColumnIdentifier(Object, Object)
Replaces the first identifier that equals oldIdentifier with newIdentifier.
 o checkAutoResort()
This method calls sort() if sortAutomatically is true
 o convertToVector(Object[])
Returns a Vector that contains the same objects as the array
 o convertToVector(Object[][])
Returns a Vector of Vectors that contains the same objects as the array
 o doCompare(int, Vector, Vector)
This method compares v1 and v2 based on the Object value in the specified column.
 o generateIds(int)
Generates a Vector which contain numColumns number of String objects.
 o getColumnCount()
 o getColumnIdentifier(int)
Returns the unique identifier object of the column at index.
 o getColumnIndex(Object)
Returns the index of the column with an unique identifier that matches the parameter columnIdentifier, using the equals() method.
 o getDataVector()
This returns the Vector of Vectors that contains the table's data values.
 o getRowCount()
 o getSortAscending()
Returns the direction of the sort.
 o getSortAutomatically()
Returns if the model is sorted automatically.
 o getSortColumn()
Returns the column to use as key for sorting the model.
 o getValueAt(int, int)
Returns an attribute value for the cell at columnIndex and rowIndex.
 o insertRow(int, Object[])
Insert a row at rowIndex in the model.
 o insertRow(int, Vector)
Insert a row at rowIndex in the model.
 o isCellEditable(int, int)
Returns true if the cell at columnIdentifier and rowIndex is editable.
 o moveRow(int, int, int)
Moves one or more rows starting at startIndex to endIndex in the model to the toIndex.
 o newDataAvailable(TableModelEvent)
This method is used to informed the model that the model's dataVector has been changed directly.
 o newRowsAdded(TableModelEvent)
This method will recalculate numRows, it will also make sure the new rows have the correct number of columns.
 o quicksort(int, int)
Quicksorts the rows of the model.
 o removeColumn(Object)
Remove the column with an unique identifier that matches the parameter columnIdentifier, using the equals() method.
 o removeRow(int)
Remove the row at rowIndex from the model.
 o rowsRemoved(TableModelEvent)
This method will recalculate numRows, then it will send out the tableRowsRemoved() notification message to all my listeners.
 o setColumnIdentifiers(Object[])
Replaces the column identifiers in the model.
 o setColumnIdentifiers(Vector)
Replaces the column identifiers in the model.
 o setDataVector(Object[][], Object[])
This replaces the value in the dataVector instance variable with the values in the array newData.
 o setDataVector(Vector, Vector)
This replaces the current dataVector instance variable with the parameter newData.
 o setNumRows(int)
Sets the number of rows in the model.
 o setSortAscending(boolean)
Sets the direction of the sorting.
 o setSortAutomatically(boolean)
Sets whether the table should sort automatically when changes are made to the model.
 o setSortColumn(int)
Sets the column to use as key for sorting the model
 o setSortColumn(Object)
Sets the column to use as key for sorting the model.
 o setValueAt(Object, int, int)
Sets the object value for the cell at columnIndex and rowIndex.
 o sort()
This method will cause all the rows of the model to be sorted based upon the instance variables sortColumn and sortAscending.
 o swap(int, int)

Variables

 o LESS_THAN
 public static final int LESS_THAN
 o EQUAL
 public static final int EQUAL
 o GREATER_THAN
 public static final int GREATER_THAN
 o dataVector
 protected Vector dataVector
The Vector of Vector of Object values

 o columnIdentifiers
 protected Vector columnIdentifiers
The Vector column identifiers

Constructors

 o DefaultTableModel
 public DefaultTableModel()
Constructs a default DefaultTableModel which is a table of zero columns and zero rows.

 o DefaultTableModel
 public DefaultTableModel(int numColumns,
                          int numRows)
Constructs a DefaultTableModel with numColumns and numRows of null object values. The columns will each be given a unique identifier that is a String of form "Column0", "Column1", "Column2", etc.

Parameters:
numColumns - The number of columns the table holds
numRows - The number of rows the table holds
See Also:
setValueAt, changeColumnIdentifier
 o DefaultTableModel
 public DefaultTableModel(Vector columnIds,
                          int numRows)
Constructs a DefaultTableModel with as many columns as there are elements in columnIds and numRows of null object values. Each column's unique identifier object will be taken from the columnIds vector.

Parameters:
columnIds - Vector containing unique identifier objects to be used to identify the columns. If this null then the model has no columns
numRows - The number of rows the table holds
See Also:
setValueAt, changeColumnIdentifier
 o DefaultTableModel
 public DefaultTableModel(Object columnIds[],
                          int numRows)
Constructs a DefaultTableModel with as many columns as there are elements in columnIds and numRows of null object values. Each column's unique identifier object will be taken from the columnIds array.

Parameters:
columnIds - Array containing unique identifier objects to be used to identify the columns. If this null then the model has no columns
numRows - The number of rows the table holds
See Also:
setValueAt, changeColumnIdentifier
 o DefaultTableModel
 public DefaultTableModel(Vector data,
                          Vector columnIds)
Constructs a DefaultTableModel and initialize the table by passing data and columnIds to the setDataVector() method. See the documentation of the getDataVector() method for details on how to structure data properly.

Parameters:
data - The data of the table
columnIds - Vector containing unique identifier objects to be used to identify the columns.
Throws: IllegalArgumentException
see setDataVector().
See Also:
getDataVector, setDataVector
 o DefaultTableModel
 public DefaultTableModel(Object data[][],
                          Object columnIds[])
Constructs a DefaultTableModel and initialize the table by passing data and columnIds to the setDataVector() method. The first index in the Object[][] is the row index and the second is the column index.

Parameters:
data - The data of the table
columnIds - Array containing unique identifier objects to be used to identify the columns.
Throws: IllegalArgumentException
see setDataVector().
See Also:
getDataVector, setDataVector

Methods

 o changeColumnIdentifier
 public void changeColumnIdentifier(Object oldIdentifier,
                                    Object newIdentifier)
Replaces the first identifier that equals oldIdentifier with newIdentifier.

Parameters:
oldIdentifier - The identifier to be replaced
newIdentifier - The new identifier
Throws: IllegalArgumentException
if oldIdentifier can't be found, or if any of the parameters are null.
 o changeColumnIdentifier
 public void changeColumnIdentifier(int columnIndex,
                                    Object newIdentifier)
Replaces the identifier of column at columnIndex with newIdentifier.

Parameters:
columnIndex - Setting the identifier of the column with this index
newIdentifier - The new identifier
Throws: IllegalArgumentException
if columnIndex is out of bounds, or if any of the parameters are null.
 o getDataVector
 public Vector getDataVector()
This 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 column 5, row 1:

((Vector)getDataVector().elementAt(1)).elementAt(5);

You can directly alter the returned Vector. You can change the cell values, the number of rows, but you may not change the number of columns in the model. If you need to alter the number of columns in the model, you can do so with addColumn(), removeColumn(), or the setDataVector() methods. Once you have finished modifying the dataVector, you must inform the model of the new data using the newDataAvailable(), rowsRemoved() or the newRowsAdded() methods. Depending on which one of the three best describes the changes made. These methods will give the model a chance to modify its internal variables based upon the new data vector. The new data methods will also generate the appropriate TableModelListener messages to notify any listeners of this model.

See Also:
newDataAvailable, newRowsAdded, rowsRemoved, setDataVector
 o setDataVector
 public void setDataVector(Vector newData,
                           Vector columnIds)
This replaces the current dataVector instance variable with the parameter newData. columnIds are the unique identifiers of the new columns. The first identifier in columnIds is mapped to column 1 in newData.

The size of the columnIds vector must equal the size of the newData vector. If columnIds is null then it will reuse the currect vector of column identifiers. Finally, this method calls newDataAvailable() to generate the appropriate notification.

Parameters:
newData - The new data vector
columnIds - The corresponding unique column identifiers for the new data vector
Throws: IllegalArgumentException
if newData is null or if the number of columns in newData does not equal the number of the column identifiers in columnIds.
See Also:
newDataAvailable, getDataVector
 o setDataVector
 public void setDataVector(Object newData[][],
                           Object columnIds[])
This replaces the value in the dataVector instance variable with the values in the array newData. The first index in the Object[][] array is the row index and the second is the column index. columnIds are the unique identifiers of the new columns.

The length of the columnIds array must equal the length of the newData array. If columnIds is null then it will reuse the currect vector of column identifiers. Finally, this method calls newDataAvailable() to generate the appropriate notification.

Parameters:
newData - The new data array
columnIds - The corresponding unique column identifiers for the new data array
Throws: IllegalArgumentException
if newData is null or if the number of columns in newData does not equal the number of the column identifiers in columnIds.
See Also:
newDataAvailable, getDataVector
 o newDataAvailable
 public void newDataAvailable(TableModelEvent event)
This method is used to informed the model that the model's dataVector has been changed directly. The event describes the extend of the update. This method will send the tableChanged() notification message to all my listeners.

If event is null or if the number of rows in dataVector is different from numRows, then it will assume all the data in dataVector is new or updated. It will recalculate numRows, and will generate the correct TableModelEvent to send out with the nofication.

Parameters:
eter - event This TableModelEvent describes where the change happened. If null it assumes all data is new or updated
See Also:
getDataVector, newRowsAdded, rowsRemoved
 o newRowsAdded
 public void newRowsAdded(TableModelEvent event)
This method will recalculate numRows, it will also make sure the new rows have the correct number of columns. Then it will send out the tableRowsInserted() notification message to all my listeners.

Parameters:
eter - event This TableModelEvent describes where the rows were added. If null it assumes all the rows were newly added.
See Also:
getDataVector
 o rowsRemoved
 public void rowsRemoved(TableModelEvent event)
This method will recalculate numRows, then it will send out the tableRowsRemoved() notification message to all my listeners.

Parameters:
eter - event This TableModelEvent describes where the rows were removed.
Throws: IllegalArgumentException
if event is null
See Also:
getDataVector
 o setColumnIdentifiers
 public void setColumnIdentifiers(Vector newIdentifiers)
Replaces the column identifiers in the model. If the number of newIdentifiers is greater than the current numColumns, new columns are added to the end of each row in the model. If the number of newIdentifier is less than the current number of columns, all the extra columns at the end of a row are discarded.

This method might cause the instance variable sortColumn to change if the original sortColumn was removed. When that happens, the model might be sorted if sortAutomatically is true.

Parameters:
newIdentifiers - Vector of column identifiers. A null means setting the model to zero columns
See Also:
setNumRows
 o setColumnIdentifiers
 public void setColumnIdentifiers(Object newIdentifiers[])
Replaces the column identifiers in the model. If the number of newIdentifiers is greater than the current numColumns, new columns are added to the end of each row in the model. If the number of newIdentifier is less than the current number of columns, all the extra columns at the end of a row are discarded.

This method might cause the instance variable sortColumn to change if the original sortColumn was removed. When that happens, the model might be sorted if sortAutomatically is true.

Parameters:
newIdentifiers - Array of column identifiers. A null means setting the model to zero columns
See Also:
setNumRows
 o setNumRows
 public void setNumRows(int newSize)
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 newSize and greater are discarded.

If new rows are added then the model will be resorted if sortAutomatically is true.

Parameters:
newSize - the new number of rows
See Also:
setColumnIdentifiers
 o addColumn
 public void addColumn(Object columnIdentifier,
                       Vector columnData)
Add a column to the model. The new column will have the unique idenitifier columnIdentifier. 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 my listeners.

Parameters:
columnIdentifier - the unique identifier of the column being added
columnData - optional data of the column being added
Throws: IllegalArgumentException
if columnIdentifier is null
 o addColumn
 public void addColumn(Object columnIdentifier,
                       Object columnData[])
Add a column to the model. The new column will have the unique idenitifier columnIdentifier. 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 my listeners.

Parameters:
columnIdentifier - the unique identifier of the column being added
columnData - optional data of the column being added
Throws: IllegalArgumentException
if columnIdentifier is null
 o removeColumn
 public void removeColumn(Object columnIdentifier)
Remove the column with an unique identifier that matches the parameter columnIdentifier, using the equals() method. If the column removed is the sorting key, then the column after the removed column will become the new key. This method will send a tableChanged() notification message to all my listeners.

This method might cause the instance variable sortColumn to change if the original sortColumn was removed. When that happens, the model might be sorted if sortAutomatically is true.

Parameters:
columnIdentifier - the unique identifier of the column being removed
 o addRow
 public void addRow(Vector rowData)
Add 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. The model will also be sorted if sortAutomatically is true.

Parameters:
rowData - optional data of the row being added
 o addRow
 public void addRow(Object rowData[])
Add 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. The model will also be sorted if sortAutomatically is true.

Parameters:
rowData - optional data of the row being added
 o insertRow
 public void insertRow(int rowIndex,
                       Vector rowData)
Insert a row at rowIndex in the model. The new row will contain null values unless rowData is specified. Notification of the row being added will be generated. The model will also be sorted if sortAutomatically is true.

Parameters:
rowIndex - the row index of the row to be inserted
rowData - optional data of the row being added
Throws: ArrayIndexOutOfBoundsException
if the rowIndex was invalid.
 o insertRow
 public void insertRow(int rowIndex,
                       Object rowData[])
Insert a row at rowIndex in the model. The new row will contain null values unless rowData is specified. Notification of the row being added will be generated. The model will also be sorted if sortAutomatically is true.

Parameters:
rowIndex - the row index of the row to be inserted
rowData - optional data of the row being added
Throws: ArrayIndexOutOfBoundsException
if the rowIndex was invalid.
 o moveRow
 public void moveRow(int startIndex,
                     int endIndex,
                     int toIndex)
Moves one or more rows starting at startIndex to endIndex in the model to the toIndex. This method will send a tableChanged() notification message to all my listeners. This method will do nothing if sortAutomatically is true.

Examples of moves:

1. moveRow(1,3,5);

a|B|C|D|e|f|g|h|i|j|k - before a|e|f|B|C|D|g|h|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:
startIndex - the starting row index to be moved
endIndex - the ending row index to be moved
toIndex - the destination of the rows to be moved
Throws: ArrayIndexOutOfBoundsException
if any of the indices are out of range. Or if endIndex is less than startIndex.
 o removeRow
 public void removeRow(int rowIndex)
Remove the row at rowIndex from the model. Notification of the row being removed, rowsRemoved(), will be sent to all my listeners.

Parameters:
rowIndex - the row index of the row to be removed
Throws: ArrayIndexOutOfBoundsException
if the rowIndex was invalid.
 o setSortColumn
 public void setSortColumn(Object columnIdentifier)
Sets the column to use as key for sorting the model.

Parameters:
columnIdentifier - the identifier of the column to use for sorting
See Also:
getSortColumn
 o setSortColumn
 public void setSortColumn(int columnIndex)
Sets the column to use as key for sorting the model

Parameters:
columnIndex - the index of the column to use for sorting
Throws: ArrayIndexOutOfBoundsException
if the index was invalid.
See Also:
getSortColumn
 o getSortColumn
 public int getSortColumn()
Returns the column to use as key for sorting the model. The default is column 0, the first column of the model.

Returns:
the column index of the sorting key.
See Also:
setSortColumn
 o setSortAscending
 public void setSortAscending(boolean ascending)
Sets the direction of the sorting. If ascending is true then the sorting will place the smallest value at the top and the largest value at the bottom.

Parameters:
ascending - The new direction of the sort
See Also:
getSortAscending
 o getSortAscending
 public boolean getSortAscending()
Returns the direction of the sort. The default is true.

Returns:
the direction of the sort.
See Also:
setSortAscending
 o setSortAutomatically
 public void setSortAutomatically(boolean autoSort)
Sets whether the table should sort automatically when changes are made to the model.

Parameters:
autoSort - true if the model should sort automatically
See Also:
getSortAutomatically
 o getSortAutomatically
 public boolean getSortAutomatically()
Returns if the model is sorted automatically. If true then the the model will automatically resort the table any time change is made to the values of the model that can effect the ordering of the rows. eg. new rows added, or values in the key column was changed. If false, no sorting will take place except when the user calls sort() directly. The default is false.

Returns:
if the model is sorted automatically.
See Also:
setSortAutomatically
 o sort
 public boolean sort()
This method will cause all the rows of the model to be sorted based upon the instance variables sortColumn and sortAscending. Use this method to sort the model if sortAutomatically is false. If sortAutomatically is true then this will be call automatically whenever the data in the model changes. If any rows of the model were changed then a tableChanged() notification is generated.

Returns:
true if the model was changed.
 o getRowCount
 public int getRowCount()
Returns:
the number of rows in the model.
Overrides:
getRowCount in class AbstractTableModel
 o getColumnCount
 public int getColumnCount()
Returns:
the number of columns in the model.
Overrides:
getColumnCount in class AbstractTableModel
 o getColumnIndex
 public int getColumnIndex(Object columnIdentifier)
Returns the index of the column with an unique identifier that matches the parameter columnIdentifier, using the equals() method. This returns -1 if columnIdentifier is null or if no match is found.

Parameters:
columnIdentifier - the identifier of the column we are looking for
Returns:
the column index of column matching columnIdentifier, or -1.
Overrides:
getColumnIndex in class AbstractTableModel
 o getColumnIdentifier
 public Object getColumnIdentifier(int index)
Returns the unique identifier object of the column at index.

Returns:
The identifier object of the column at index
Throws: ArrayIndexOutOfBoundsException
if the index was invalid.
Overrides:
getColumnIdentifier in class AbstractTableModel
 o isCellEditable
 public boolean isCellEditable(int rowIndex,
                               int columnIndex)
Returns true if the cell at columnIdentifier and rowIndex is editable. Otherwise, the setValueAt() on the cell will not change the value of that cell.

Parameters:
rowIndex - the row whose value is to be looked up
columnIndex - the column whose value is to be looked up
Returns:
true if the cell is editable.
Overrides:
isCellEditable in class AbstractTableModel
See Also:
setValueAt
 o getValueAt
 public Object getValueAt(int rowIndex,
                          int columnIndex)
Returns an attribute value for the cell at columnIndex and rowIndex.

Parameters:
rowIndex - the row whose value is to be looked up
columnIndex - the column whose value is to be looked up
Returns:
the value Object at the specified cell
Throws: ArrayIndexOutOfBoundsException
if an invalid row index or columnIndex was given.
Overrides:
getValueAt in class AbstractTableModel
 o setValueAt
 public void setValueAt(Object aValue,
                        int rowIndex,
                        int columnIndex)
Sets the object value for the cell at columnIndex and rowIndex. aValue is the new value. This method will generate a tableChanged() notification. Also the model will be sorted if the cell is in the sortColumn and sortAutomatically is true.

Parameters:
aValue - the new value. This can be null.
rowIndex - the row whose value is to be changed
columnIndex - the column whose value is to be changed
Throws: ArrayIndexOutOfBoundsException
if an invalid row index or columnIndex was given.
Overrides:
setValueAt in class AbstractTableModel
 o checkAutoResort
 protected boolean checkAutoResort()
This method calls sort() if sortAutomatically is true

 o quicksort
 protected void quicksort(int left,
                          int right)
Quicksorts the rows of the model. This code is adapted from JDK1.1's SortDemo.

Parameters:
left - - the start index - from where to begin sorting
right - - the last index.
See Also:
doCompare
 o doCompare
 protected int doCompare(int column,
                         Vector v1,
                         Vector v2)
This method compares v1 and v2 based on the Object value in the specified column. If both Objects in the columns are subclass of Number, then it will do the compare based upon the numeric value. If both are Booleans, then it will do the compare with true being greater than false. Date objects are compared using the methods before(), equal(), and after(). Finally, all other Object types will be compared based on their toString() values compared as Strings. If the column objects in v1 and v2 are different types then they will be compared using their toString() values also. null objects are considered to be less than any non-null objects.

Subclass can override this method to extend the model to correctly sort other types of Objects.

Returns:
the value 0 if v1 is equal to v2; a value less than 0 if v1 is less than v2; and a value greater than 0 if v1 is greater than v2
See Also:
sort
 o swap
 protected void swap(int i,
                     int j)
 o generateIds
 protected static Vector generateIds(int numColumns)
Generates a Vector which contain numColumns number of String objects. The String are of the form, "Column0", "Column1", etc.

 o convertToVector
 protected static Vector convertToVector(Object anArray[])
Returns a Vector that contains the same objects as the array

 o convertToVector
 protected static Vector convertToVector(Object anArray[][])
Returns a Vector of Vectors that contains the same objects as the array


All Packages  Class Hierarchy  This Package  Previous  Next  Index