Java Reference
In-Depth Information
public DefaultTableModel(Object columnNames[], int rows)
TableModel model = new DefaultTableModel(columnNames, 2);
public DefaultTableModel(Vector columnNames, int rows)
TableModel model = new DefaultTableModel(columnNames, 2);
Four of the constructors map directly to JTable constructors, whereas the remaining two
allow you to create empty tables from a set of column headers with a fixed number of rows.
Once you've created the DefaultTableModel , you pass it along to a JTable constructor to create
the actual table, and then place the table in a JScrollPane .
Filling a DefaultTableModel
If you choose to use a DefaultTableModel , you must fill it with data for your JTable to display
anything. Along with basic routines to fill the data structure, there are additional methods to
remove data or replace the entire contents:
The following methods allow you to add columns:
public void addColumn(Object columnName);
public void addColumn(Object columnName, Vector columnData);
public void addColumn(Object columnName, Object columnData[ ]);
Use these methods to add rows:
public void addRow(Object rowData[ ]);
public void addRow(Vector rowData);
These methods insert rows:
public void insertRow(int row, Object rowData[ ]);
public void insertRow(int row, Vector rowData);
This method removes rows:
public void removeRow( int row);
And finally, you can replace contents with the following two methods:
public void setDataVector(Object newData[ ][ ], Object columnNames[ ]);
public void setDataVector(Vector newData, Vector columnNames);
DefaultTableModel Properties
In addition to the rowCount and columnCount properties inherited from AbstractTableModel ,
DefaultTableModel has two other properties, as shown in Table 18-6. Setting the rowCount
property allows you to enlarge or shrink the table size as you please. If you are expanding the
model, the additional rows remain empty.
Search WWH ::




Custom Search