The JTable is used to display and edit regular two-dimensional tables
of cells.
The JTable has many
facilities that make it possible to customize its rendering and editing
but provides defaults for these features so that simple tables can be
set up easily. For example, to set up a table with 10 rows and 10
columns of numbers:
class MyTableModel extends AbstractTableModel{
public function getColumnCount():int { return 10; }
public function getRowCount():int { return 10;}
public getValueAt(row:int, col:int) { return row*col; }
};
var dataModel:MyTableModel = new MyTableModel();
var table:JTable = new JTable(dataModel);
var scrollpane:JScrollPane = new JScrollPane(table);
Note that if you wish to use a JTable in a standalone
view (outside of a JScrollPane ) and want the header
displayed, you can get it using {@link #getTableHeader} and
display it separately.
When designing applications that use the JTable it is worth paying
close attention to the data structures that will represent the table's data.
The DefaultTableModel is a model implementation that
uses a Vector of Vector s of Object s to
store the cell values. As well as copying the data from an
application into the DefaultTableModel ,
it is also possible to wrap the data in the methods of the
TableModel interface so that the data can be passed to the
JTable directly, as in the example above. This often results
in more efficient applications because the model is free to choose the
internal representation that best suits the data.
A good rule of thumb for deciding whether to use the AbstractTableModel
or the DefaultTableModel is to use the AbstractTableModel
as the base class for creating subclasses and the DefaultTableModel
when subclassing is not required.
The JTable uses integers exclusively to refer to both the rows and the columns
of the model that it displays. The JTable simply takes a tabular range of cells
and uses getValueAt(int, int) to retrieve the
values from the model during painting.
By default, columns may be rearranged in the JTable so that the
view's columns appear in a different order to the columns in the model.
This does not affect the implementation of the model at all: when the
columns are reordered, the JTable maintains the new order of the columns
internally and converts its column indices before querying the model.
To listen table row selection change event, by this.addSelectionListener()
To listen column selection see org.aswing.table.TableColumnModel#addColumnModelListener
To listen other Table events see org.aswing.table.TableModel#addTableModelListener
@author paling |