Download the JDK
Search the Tutorials
Hide the TOC
Trail: Creating a GUI with JFC/Swing
Lesson: Writing Event Listeners
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
Solving Common Event-Handling Problems

> This section discusses problems that you might encounter while handling events.

> Problem: I'm trying to handle certain events from a component, but the component isn't generating the events it should.

> - First, make sure you registered the right kind of listener to detect the events. See whether another kind of listener might detect the kind of events you need.
> - Make sure you registered the listener on the right object.
> - Did you implement the event handler correctly? For example, if you extended an adapter class, then make sure you used the right method signature. Make sure each event-handling method is `public void`, that the name spelled right and that the argument is of the right type.
> - If you still think that the component isn't generating the events it should, check the Java Developer Connection to see whether this is a known bug.

> Problem: My combo box isn't generating low-level events such as focus events.

> - Combo boxes are compound components — components implemented using multiple components. For this reason, combo boxes don't fire the low-level events that simple components fire. For more information, see Handling Events on a Combo Box.

> Problem: The document for an editor pane (or text pane) isn't firing document events.

> - The document instance for an editor pane or text pane might change when loading text from a URL. Thus, your listeners might be listening for events on an unused document. For example, if you load an editor pane or text pane with HTML that was previously loaded with plain text, the document will change to an `HTMLDocument` instance. If your program dynamically loads text into an editor pane or text pane, make sure the code adjusts for possible changes to the document (re-register document listeners on the new document, and so on).
> If you don't see your problem in this list, see Solving Common Component Problems.

Previous page: Listener API Table
Next page: Questions and Exercises: Writing Event Listeners