com.jgoodies.binding.adapter
Class AbstractTableAdapter<E>
AbstractTableModel
com.jgoodies.binding.adapter.AbstractTableAdapter<E>
public abstract class AbstractTableAdapter<E>
extends AbstractTableModel
An abstract implementation of the
javax.swing.table.TableModel
interface that converts a
javax.swing.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 {
private static final String[] COLUMN_NAMES =
{ "Last Name", "First Name", "Age" };
public CustomerTableModel(ListModel listModel) {
super(listModel, COLUMN_NAMES);
}
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;
}
}
}
javax.swing.ListModel
, javax.swing.JTable
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.
|
@Override | String getColumnName(int columnIndex) - Returns the name of the column at the given column index.
|
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.
|
E | getRow(int index) - Returns the row at the specified row index.
|
int | getRowCount() - Returns the number of rows in the model.
|
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)
.
listModel
- the ListModel that holds the row elements
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)
.
listModel
- the ListModel that holds the row elementscolumnNames
- optional column names
String getColumnName
public @Override 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.
columnIndex
- the index of the column
createChangeHandler
protected ListDataListener createChangeHandler()
Creates and returns a listener that handles changes
in the underlying list model.
- the listener that handles changes in the underlying ListModel
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.
- the number of columns in the model
getRow
public final E getRow(int index)
Returns the row at the specified row index.
index
- row index in the underlying list model
- the row at the specified row index.
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.
- the number of rows in the model
Copyright © 2002-2007 JGoodies Karsten Lentzsch. All Rights Reserved.