ListTableModel

Top  Previous  Next

 

We extended the TableModel interface with ListTableModel, that requires the tabular data to be kept in a list structure. ListTableModel is the interface that is inherited by other TableModels in our library to provide effects such as sorting (SortTableModel), filtering (FilterTableModel) or tree-like viewing (TreeTableModel). ListTableModel extends the TableModel interface and also defines methods for manipulating the data of a tabular data model.

 

By implementing ListTableModel in your TableModel, it is assumed that the data are stored in a collection indexed by row number. This collection is returned with the method:

 

public java.util.List getRows();

 

Hence, if we want to get the object at the 1st row:

 

Object rowObject = ListTableModel.getRows().get(0);

 

Some additional methods are used to add and remove elements in the list. These are:

 

public void addRow(Object row);

public void addRows(List addedRows);

public void clear();

public void removeRow(int row);

public void removeRows(int [] deletedRows);

 

The elements in the list represent the actual row data, which can be any Java object. To get a reference to a specific column of a row data, use:

 

public Object getCellValue(Object row, int index);

 

For example, for a DefaultTableModel subclass implementing ListTableModel, the method's body would be:

 

public Object getCellValue(Object row, int index) {

       return ((Vector) row).get(index);

};

 

The TableModels in the library (SortTableModel, FilterTableModel and TreeTableModel) all require a ListTableModel in their constructor. Therefore, in order to use your own custom TableModel in conjunction with these classes, your TableModel should implement the ListTableModel interface. This can be very easily done. Please look in Appendix I, where the source code listing for a ListTableModel implementation that extends javax.swing.table.DefaultTableModel is presented.