The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Trail: Creating a GUI with JFC/Swing
Lesson: Writing Event Listeners

How to Write a Table Model Listener

Each JTable(in the Creating a GUI with JFC/Swing trail) object has a table model that holds its data. When a table model listener is registered on the table model, the listener is notified every time the table model's data changes. The JTable itself automatically uses a table model listener to make its GUI reflect the current state of the table model. You register a table model listener using the TableModel addTableModelListener method.

The Table Model Event API

The TableModelListener(in the API reference documentation) interface contains a single method, and thus has no corresponding adapter class. Here is the lone TableModelListener method:
void tableChanged(TableModelEvent)
Called when the structure of or data in the table has changed.
The tableChanged method's argument is a TableModelEvent(in the API reference documentation) object that specifies which cells changed, and how in general they changed. You can query the TableModelEvent using the following methods:
int getFirstRow()
Returns the index of the first row that changed. TableModelEvent.HEADER_ROW specifies the table header.
int getLastRow()
The last row that changed. Again, HEADER_ROW is a possible value.
int getColumn()
Returns the index of the column that changed. The constant TableModelEvent.ALL_COLUMNS specifies that all the columns might have changed.
int getType()
Specifies what happened to the changed cells. The returned value is one of the following: TableModelEvent.INSERT, TableModelEvent.DELETE, or TableModelEvent.UPDATE.
Also useful is the getSource method, which TableModelEvent inherits from EventObject(in the API reference documentation).

Examples that Use Table Model Listeners

The following table lists the examples that use table model listeners.

Example Where Described Notes
TableMap(in a .java source file) Sorting and Otherwise Manipulating Data(in the Creating a GUI with JFC/Swing trail) A superclass for data-manipulating table models. It implements a table model that sits between a table data model and a JTable. The TableMap listens for table model events from the data model, and then simply forwards them to its table model listeners (such as the JTable).
TableSorter(in a .java source file) Sorting and Otherwise Manipulating Data(in the Creating a GUI with JFC/Swing trail) A sorting table model implemented as a subclass of TableMap. In addition to forwarding table model events, the tableChanged method keeps track of the number of rows.
SharedModelDemo -- Does not implement a table model listener. Instead, it implements a combined list and table model.


Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Copyright 1995-2002 Sun Microsystems, Inc. All rights reserved.