Event and listener types

All Swing components include addXXXListener( ) and removeXXXListener( ) methods so that the appropriate types of listeners can be added and removed from each component. Youll notice that the XXX in each case also represents the argument for the method, for example, addMyListener(MyListener m). The following table includes the basic associated events, listeners, and methods, along with the basic components that support those particular events by providing the addXXXListener( ) and removeXXXListener( ) methods. You should keep in mind that the event model is designed to be extensible, so you may encounter other events and listener types that are not covered in this table.

Anything derived from JTextComponent, including JTextArea and JTextField

You can see that each type of component supports only certain types of events. It turns out to be rather difficult to look up all the events supported by each component. A simpler approach is to modify the ShowMethods.java program from Chapter 10 so that it displays all the event listeners supported by any Swing component that you enter.

Chapter 10 introduced reflection and used that feature to look up methods for a particular classeither the entire list of methods or a subset of those whose names match a keyword that you provide. The magic of reflection is that it can automatically show you all the methods for a class without forcing you to walk up the inheritance hierarchy, examining the base classes at each level. Thus, it provides a valuable timesaving tool for programming; because the names of most Java methods are made nicely verbose and descriptive, you can search for the method names that contain a particular word of interest. When you find what you think youre looking for, check the JDK documentation.

However, by Chapter 10 you hadnt seen Swing, so the tool in that chapter was developed as a command-line application. Here is the more useful GUI version, specialized to look for the addListener methods in Swing components:

You enter the Swing class name that you want to look up in the name JTextField. The results are extracted using regular expressions, and displayed in a JTextArea.

Youll notice that there are no buttons or other components to indicate that you want the search to begin. Thats because the JTextField is monitored by an ActionListener. Whenever you make a change and press ENTER, the list is immediately updated. If the text field isnt empty, it is used inside Class.forName( ) to try to look up the class. If the name is incorrect, Class.forName( ) will fail, which means that it throws an exception. This is trapped, and the JTextArea is set to No match. But if you type in a correct name (capitalization counts), Class.forName( ) is successful, and getMethods( ) will return an array of Method objects.

Two regular expressions are used here. The first, addListener, looks for add followed by any word characters, followed by Listener and the argument list in parentheses. Notice that this whole regular expression is surrounded by non-escaped parentheses, which means it will be accessible as a regular expression group when it matches. Inside NameL.ActionPerformed( ), a Matcher is created by passing each Method object to the Pattern.matcher( ) method. When find( ) is called for this Matcher object, it returns true only if a match occurs, and in that case you can select the first matching parenthesized group by calling group(1). This string still contains qualifiers, so to strip them off the qualifierPattern object is used just as it was in c09:ShowMethods.java.

At the end of init( ), an initial value is placed in name and the action event is run to provide a test with initial data.

This program is a convenient way to investigate the capabilities of a Swing component. Once you know which events a particular component supports, you dont need to look anything up to react to that event. You simply:

Take the name of the event class and remove the word
Event. Add the word Listener to what
remains. This is the listener interface you must implement in your inner class.

Implement the interface above and write out the methods for the events you
want to capture. For example, you might be looking for mouse movements, so you
write code for the mouseMoved( ) method of the
MouseMotionListener interface. (You must implement the other methods, of
course, but theres often a shortcut for this, which youll see
soon.)

Create an object of the listener class in Step 2. Register it with your
component with the method produced by prefixing add to your
listener name. For example, addMouseMotionListener( ).

This is not an exhaustive listing, partly because the event model allows you to create your own event types and associated listeners. Thus, youll regularly come across libraries that have invented their own events, and the knowledge gained in this chapter will allow you to figure out how to use these events.

Using listener adapters for simplicity

In the table above, you can see that some listener interfaces have only one method. These are trivial to implement, because youll implement them only when you want to write that particular method. However, the listener interfaces that have multiple methods can be less pleasant to use. For example, if you want to capture a mouse click (that isnt already captured for you, for example, by a button), then you need to write a method for mouseClicked( ). But since MouseListener is an interface, you must implement all of the other methods even if they dont do anything. This can be annoying.

To solve the problem, some (but not all) of the listener interfaces that have more than one method are provided with adapters, the names of which you can see in the table above. Each adapter provides default empty methods for each of the interface methods. Then all you need to do is inherit from the adapter and override only the methods you need to change. For example, the typical MouseListener youll use looks like this:

This doesnt work, but it will drive you crazy trying to figure out why, since everything will compile and run fineexcept that your method wont be called for a mouse click. Can you see the problem? Its in the name of the method: MouseClicked( ) instead of mouseClicked ( ). A simple slip in capitalization results in the addition of a completely new method. However, this is not the method thats called when the window is closing, so you dont get the desired results. Despite the inconvenience, an interface will guarantee that the methods are properly implemented.