Java Reference
In-Depth Information
Table 18-7.
TableModelEvent Properties
Property Name
Data Type
Access
column
int
Read-only
firstRow
int
Read-only
lastRow
int
Read-only
type
int
Read-only
The event type can be one of three type constants of
TableModelEvent
:
INSERT
,
UPDATE
,
or
DELETE
.
If the
column
property setting for the
TableModelEvent
is
ALL_COLUMNS
, then all the columns
in the data model are affected. If the
firstRow
property is
HEADER_ROW
, it means the table header
changed.
Sorting JTable Elements
The
JTable
component doesn't come with built-in support for sorting. Nevertheless, this
feature is frequently requested. Sorting doesn't require changing the data model, but it does
require changing the view of the data model that the
JTable
has. This type of change is described
by the Decorator pattern, in which you maintain the same API to the data but add sorting capa-
bilities to the view. The participants of the Decorator design pattern are as follows:
•
Component
: The component defines the service interface that will be decorated.
•
ConcreteComponent
: The concrete component is the object to be decorated.
•
Decorator
: The decorator is an abstract wrapper to a concrete component; it maintains
the service interface.
•
ConcreteDecorator
(s) [
A
,
B
,
C
, . . .]: The concrete decorator objects extend the decorator
by adding decorating responsibilities while maintaining the same programming interface.
They redirect service requests to the concrete component referred to by their abstract
superclass.
■
Note
The streams of the
java.io
package are examples of the Decorator pattern. The various filter
streams add capabilities to the basic stream classes and maintain the same API for access.
In the particular case for table sorting, only the
Component
,
ConcreteComponent
, and
Decorator
are required, because there is only one concrete decorator. The
Component
is the
TableModel
interface, the
ConcreteComponent
is the actual model, and the
Decorator
is the
sorted model.