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) {
            // do something
}

There are a couple of different ways to do this. Please see the blog on Inner Classes earlier in the book for a discussion and demonstration of implementing action listeners. Please see the Toolkit in the back of this book for numerous, complete, working examples of handling action events.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.183.150