|
The JDK 1.1 event handling model provides event listener interfaces and adapter classes for setting up event listening functionality in your programs. Whether you choose to implement the interfaces, use the adapter classes, or combine interface implementations with adapter classes depends on your application requirements. Event Listener InterfacesIn the class declaration, declare the event listener interfaces for the events you need. With this approach, you have to implement all the interface methods for the interfaces you declare. If an event listener interface has methods not used by the program, consider using the corresponding adapter class instead. The following code makes the LayoutExample class an action listener by implementing the ActionListener interface. An action listener responds with its actionPerformed() implementation when the end user clicks a button, double clicks a list item, makes a menu selection, or presses Return in a text field. The actionPerfomed() implementation handles all such events for components you explicitly add as event listeners (see below).
Adding Components as Event ListenersYou have to add components as event listeners. The component classes have the appropriate add methods for this purpose. For example, Button, List, MenuItem, and TextField objects have an addActionListener method, and all Container objects have addComponentListener, addFocusListener, addInputMethodListener, addKeyListener, addMouseListener, and addMouseMotionListener methods. The following excerpted code shows how to add a button as an action listener. Click here for the complete program.
Because LayoutExample is a subclass of java.awt.Frame, you can add the window closing behavior by implementing the java.awt.Frame.addWindowListener() method as shown in the next code segment. If you extend the class from applet instead of frame, there is no need to add a window listener. In fact, the applet class has no methods for adding window listeners because window listening functionality is built in.
Source CodeThe following source files contain the JDK 1.0 and 1.1 versions of the application code for the demonstration application used in the Exploring the AWT Layout Managers article. The only difference between the two versions is how event handling is coded. The JDK 1.1 applet source code is the applet version of the JDK 1.1 source code. The only differences between the application and applet versions are that applets do not have main methods and the window closing behavior is built in.
The following source files contain the JDK 1.1 application with the event handling code in a separate non-component class.
Initial GUI, and Using Adapter ClassesMany event listener interfaces contain more than one method. For example, the MouseListener interface, which responds to end user mouse or other similar input device actions, has five methods: mousePressed(), mouseReleased(), mouseEntered(), mouseExited(), and mouseClicked(). If you implement the MouseListener interface, you have to implement all five methods even if you only need one. To get around having a lot of empty method implementations cluttering your code, you can extend MouseAdapter and implement only the methods you need:
Using Inner ClassesAdapter classes have to be extended to be used, and sometimes your class already extends another class. The Java language does not allow multiple inheritance, so in this case you have to use the adapter class as an inner class. While a class can extend only one class, it can implement as many interfaces and use as many inner classes as it needs. The example below shows the MouseExample class extending Frame and using AMouseAdapter as an inner class that extends MouseAdapter.
The following applet code taken from The Java Tutorial: Object Oriented Programming for the Internet. uses an inner class to implement the MouseAdapter class.
ConclusionThe JDK 1.1 event model is more flexible, more powerful, and easier to use than the JDK 1.0 event model. Upgrading your JDK 1.0 code makes it possible for you to take advantage of new features and Swing. Once you convert to the new event model, you can convert your Abstract Window Toolkit (AWT) components to Swing components.
| ||||||||||||||||
|
| ||||||||||||