Download the JDK
Search the Tutorials
Hide the TOC
Trail: Creating a GUI with JFC/Swing
Lesson: Writing Event Listeners
Section: Implementing Listeners for Commonly Handled Events
Writing Event Listeners
Introduction to Event Listeners
General Information about Writing Event Listeners
Listeners Supported by Swing Components
Implementing Listeners for Commonly Handled Events
How to Write an Action Listener
How to Write a Caret Listener
How to Write a Change Listener
How to Write a Component Listener
How to Write a Container Listener
How to Write a Document Listener
How to Write a Focus Listener
How to Write an Internal Frame Listener
How to Write an Item Listener
How to Write a Key Listener
How to Write a List Data Listener
How to Write a List Selection Listener
How to Write a Mouse Listener
How to Write a Mouse-Motion Listener
How to Write a Mouse-Wheel Listener
How to Write a Property Change Listener
How to Write a Table Model Listener
How to Write a Tree Expansion Listener
How to Write a Tree Model Listener
How to Write a Tree Selection Listener
How to Write a Tree-Will-Expand Listener
How to Write an Undoable Edit Listener
How to Write Window Listeners
Listener API Table
Solving Common Event-Handling Problems
Home Page > Creating a GUI with JFC/Swing > Writing Event Listeners
« Previous • Trail • Next »
How to Write a Table Model Listener

> Each `JTable` 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 Listener API

> The TableModelListener Interface

> Because `TableModelListener` has only one method, it has no corresponding adapter class.

> Method | Purpose
> --- | ---
> tableChanged(TableModelEvent) | Called when the structure of or data in the table has changed.

> The TableModelEvent API

> Method | Purpose
> --- | ---
> Object getSource() (in `java.util.EventObject`) | Return the object that fired the event.
> int getFirstRow() | Return 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() | Return the index of the column that changed. The constant `TableModelEvent.ALL_COLUMNS` specifies that all the columns might have changed.
> int getType() | What happened to the changed cells. The returned value is one of the following: `TableModelEvent.INSERT`, `TableModelEvent.DELETE`, or `TableModelEvent.UPDATE`.

### Examples that Use Table Model Listeners

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

> Example | Where Described | Notes
> --- | --- | ---
> `TableMap` | Sorting and Otherwise Manipulating Data | 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` | Sorting and Otherwise Manipulating Data | 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 • Trail • Next »
Previous page: How to Write a Property Change Listener
Next page: How to Write a Tree Expansion Listener