Java Reference
In-Depth Information
Table 18-6. DefaultTableModel Properties
Property Name
Data Type
Access
columnCount
int
Read-only
columnIdentifiers
Vector
Write-only
dataVector
Vector
Read-only
rowCount
int
Read-write
Creating a Sparse Table Model
The default table model implementations are meant for tables that are full of data, not for
spreadsheets consisting of mostly empty cells. When the cells in the table are mostly empty,
the default data structure for the DefaultTableModel will end up with plenty of wasted space.
At the cost of creating a Point for each lookup, you can create a sparse table model that can use
a HashMap underneath it. Listing 18-5 demonstrates one such implementation.
Listing 18-5. Sparsely Populated Table Model
import java.awt.Point;
import java.util.HashMap;
import java.util.Map;
import javax.swing.table.AbstractTableModel;
public class SparseTableModel extends AbstractTableModel {
static final long serialVersionUID = 5774430402629969511L;
private Map<Point, Object> lookup;
private final int rows;
private final int columns;
private final String headers[];
public SparseTableModel(int rows, String columnHeaders[]) {
if ((rows < 0) || (columnHeaders == null)) {
throw new IllegalArgumentException("Invalid row count/columnHeaders");
}
this.rows = rows;
this.columns = columnHeaders.length;
headers = columnHeaders;
lookup = new HashMap<Point, Object>();
}
 
Search WWH ::




Custom Search