com.jgoodies.binding.adapter
Class AbstractTableAdapter<E>

java.lang.Object
  extended by javax.swing.table.AbstractTableModel
      extended by com.jgoodies.binding.adapter.AbstractTableAdapter<E>
Type Parameters:
E - the type of the ListModel elements
All Implemented Interfaces:
ListModelBindable, Serializable, TableModel

public abstract class AbstractTableAdapter<E>
extends AbstractTableModel
implements ListModelBindable

An abstract implementation of the TableModel interface that converts a ListModel of row elements.

This class provides default implementations for the TableModel methods #getColumnCount() and #getColumnName(int). To use these methods you must use the constructor that accepts an array of column names and this array must not be null. If a subclass constructs itself with the column names set to null it must override the methods #getColumnCount() and #getColumnName(int).

Example: API users subclass AbstractTableAdapter and just implement the method TableModel#getValueAt(int, int).

The following example implementation is based on a list of customer rows and exposes the first and last name as well as the customer ages:

 public class CustomerTableModel extends AbstractTableAdapter {

     public CustomerTableModel() {
         super("Last Name", "First Name", "Age");
     }

     public Object getValueAt(int rowIndex, int columnIndex) {
         Customer customer = (Customer) getRow(rowIndex);
         switch (columnIndex) {
             case 0 : return customer.getLastName();
             case 1 : return customer.getFirstName();
             case 2 : return customer.getAge();
             default: return null;
         }
     }

 }
 

Version:
$Revision: 1.16 $
Author:
Karsten Lentzsch
See Also:
ListModel, JTable, Serialized Form

Field Summary
 
Fields inherited from class javax.swing.table.AbstractTableModel
listenerList
 
Constructor Summary
AbstractTableAdapter()
          Constructs an AbstractTableAdapter with no ListModel set and no predefined column names.
AbstractTableAdapter(ListModel listModel)
          Constructs an AbstractTableAdapter on the given ListModel.
AbstractTableAdapter(ListModel listModel, String... columnNames)
          Constructs an AbstractTableAdapter on the given ListModel using the specified table column names.
AbstractTableAdapter(String... columnNames)
          Constructs an AbstractTableAdapter with the given column names.
 
Method Summary
protected  ListDataListener createChangeHandler()
          Creates and returns a listener that handles changes in the underlying list model.
 int getColumnCount()
          Returns the number of columns in the model.
 String getColumnName(int columnIndex)
          Returns the name of the column at the given column index.
 ListModel getListModel()
           
 E getRow(int index)
          Returns the row at the specified row index.
 int getRowCount()
          Returns the number of rows in the model.
 void setListModel(ListModel newListModel)
          Sets the given ListModel as new underlying ListModel.
 
Methods inherited from class javax.swing.table.AbstractTableModel
addTableModelListener, findColumn, fireTableCellUpdated, fireTableChanged, fireTableDataChanged, fireTableRowsDeleted, fireTableRowsInserted, fireTableRowsUpdated, fireTableStructureChanged, getColumnClass, getListeners, getTableModelListeners, isCellEditable, removeTableModelListener, setValueAt
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface javax.swing.table.TableModel
getValueAt
 

Constructor Detail

AbstractTableAdapter

public AbstractTableAdapter()
Constructs an AbstractTableAdapter with no ListModel set and no predefined column names.

Since:
2.2

AbstractTableAdapter

public AbstractTableAdapter(ListModel listModel)
Constructs an AbstractTableAdapter on the given ListModel. Subclasses that use this constructor must override the methods #getColumnCount() and #getColumnName(int).

Parameters:
listModel - the ListModel that holds the row elements

AbstractTableAdapter

public AbstractTableAdapter(String... columnNames)
Constructs an AbstractTableAdapter with the given column names.

Parameters:
columnNames - the predefined column names
Since:
2.2

AbstractTableAdapter

public AbstractTableAdapter(ListModel listModel,
                            String... columnNames)
Constructs an AbstractTableAdapter on the given ListModel using the specified table column names. If the column names array is non-null, it is copied to avoid external mutation.

Subclasses that invoke this constructor with a null column name array must override the methods #getColumnCount() and #getColumnName(int).

Parameters:
listModel - the ListModel that holds the row elements
columnNames - optional column names
Method Detail

getListModel

public ListModel getListModel()
Specified by:
getListModel in interface ListModelBindable
Returns:
the underlying ListModel
Since:
2.2

setListModel

public void setListModel(ListModel newListModel)
Sets the given ListModel as new underlying ListModel. Removes the list data listener from the previously set ListModel - if any - and adds it to the new ListModel.

Specified by:
setListModel in interface ListModelBindable
Parameters:
newListModel - the ListModel to be set
Since:
2.2

getColumnCount

public int getColumnCount()
Returns the number of columns in the model. A JTable uses this method to determine how many columns it should create and display by default.

Subclasses must override this method if they don't provide an array of column names in the constructor.

Specified by:
getColumnCount in interface TableModel
Returns:
the number of columns in the model
Throws:
NullPointerException - if the optional column names array has not been set in the constructor. In this case API users must override this method.
See Also:
getColumnName(int), getRowCount()

getColumnName

public String getColumnName(int columnIndex)
Returns the name of the column at the given column index. This is used to initialize the table's column header name. Note: this name does not need to be unique; two columns in a table can have the same name.

Subclasses must override this method if they don't provide an array of column names in the constructor.

Specified by:
getColumnName in interface TableModel
Overrides:
getColumnName in class AbstractTableModel
Parameters:
columnIndex - the index of the column
Returns:
the name of the column
Throws:
NullPointerException - if the optional column names array has not been set in the constructor. In this case API users must override this method.
See Also:
getColumnCount(), getRowCount()

getRowCount

public final int getRowCount()
Returns the number of rows in the model. A JTable uses this method to determine how many rows it should display. This method should be quick, as it is called frequently during rendering.

Specified by:
getRowCount in interface TableModel
Returns:
the number of rows in the model
See Also:
getRow(int)

getRow

public final E getRow(int index)
Returns the row at the specified row index.

Parameters:
index - row index in the underlying list model
Returns:
the row at the specified row index.

createChangeHandler

protected ListDataListener createChangeHandler()
Creates and returns a listener that handles changes in the underlying list model.

Returns:
the listener that handles changes in the underlying ListModel


Copyright © 2002-2013 JGoodies Software GmbH. All Rights Reserved.