Handling Action Events

Examples in the Toolkit, such as the GaragePad doodler, show how to capture mouse motion events. There are also examples in the doodler application and the text editor that show how to handle action events from menu items when they're clicked.

In Swing, you create user controls for the user to interact with. When the user does something to a control—for example, clicks a button—that action fires an event. You create listeners that do something when that event is fired, and then attach them to the user controls. Actions can optionally hold information about tool tip text or icons, though this is rare.

Specifically, to handle an event you override the actionPerformed() method.

 public void actionPerformed(ActionEvent evt) ...

Get Java Garage now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.