Adapter Classes

Many event-listener interfaces, such as MouseListener and MouseMotionListener, contain multiple methods. It is not always desirable to declare every method in an event-listener interface. For instance, an application may need only the mouseClicked handler from MouseListener or the mouseDragged handler from MouseMotionListener. Interface WindowListener specifies seven window event-handling methods. For many of the listener interfaces that have multiple methods, packages java.awt.event and javax.swing.event provide event-listener adapter classes. An adapter class implements an interface and provides a default implementation (with an empty method body) of each method in the interface. Figure 11.30 shows several java.awt.event adapter classes and the interfaces they implement. You can extend an adapter class to inherit the default implementation of every method and subsequently override only the method(s) you need for event handling.

Figure 11.30. Event-adapter classes and the interfaces they implement in package java.awt.event.

(This item is displayed on page 557 in the print version)

Event-adapter class in java.awt.event

Implements interface

ComponentAdapter

ComponentListener

ContainerAdapter

ContainerListener

FocusAdapter

FocusListener

KeyAdapter

KeyListener

MouseAdapter

MouseListener

MouseMotionAdapter

MouseMotionListener

WindowAdapter

WindowListener

Software Engineering Observation 11.7

When a class implements an interface, the class has an "is a" relationship with that interface. All direct and indirect subclasses of that class inherit this interface. Thus, an object of a class that extends an event-adapter class is an object of the corresponding event-listener type (e.g., an object of a subclass of MouseAdapter is a MouseListener).

 

Extending MouseAdapter

The application of Fig. 11.31 and Fig. 11.32 demonstrates how to determine the number of mouse clicks (i.e., the click count) and how to distinguish between the different mouse buttons. The event listener in this application is an object of inner class MouseClickHandler (lines 2646) that extends MouseAdapter, so we can declare just the mouseClicked method we need in this example.

Figure 11.31. Left, center and right mouse-button clicks.

(This item is displayed on pages 557 - 558 in the print version)

1 // Fig. 11.31: MouseDetailsFrame.java 2 // Demonstrating mouse clicks and distinguishing between mouse buttons. 3 import java.awt.BorderLayout; 4 import java.awt.Graphics; 5 import java.awt.event.MouseAdapter; 6 import java.awt.event.MouseEvent; 7 import javax.swing.JFrame; 8 import javax.swing.JLabel; 9 10 public class MouseDetailsFrame extends JFrame 11 { 12 private String details; // String representing 13 private JLabel statusBar; // JLabel that appears at bottom of window 14 15 // constructor sets title bar String and register mouse listener 16 public MouseDetailsFrame() 17 { 18 super( "Mouse clicks and buttons" ); 19 20 statusBar = new JLabel( "Click the mouse" ); 21 add( statusBar, BorderLayout.SOUTH ); 22 addMouseListener( new MouseClickHandler() ); // add handler 23 } // end MouseDetailsFrame constructor 24 25 // inner class to handle mouse events 26 private class MouseClickHandler extends MouseAdapter 27 { 28 // handle mouse click event and determine which button was pressed 29 public void mouseClicked( MouseEvent event ) 30 { 31 int xPos = event.getX(); // get x position of mouse 32 int yPos = event.getY(); // get y position of mouse 33 34 details = String.format( "Clicked %d time(s)", 35 event.getClickCount() ); 36 37 if ( event.isMetaDown() ) // right mouse button 38 details += " with right mouse button"; 39 else if ( event.isAltDown() ) // middle mouse button 40 details += " with center mouse button"; 41 else // left mouse button 42 details += " with left mouse button"; 43 44 statusBar.setText( details ); // display message in statusBar 45 } // end method mouseClicked 46 } // end private inner class MouseClickHandler 47 } // end class MouseDetailsFrame

Figure 11.32. Test class for MouseDetailsFrame.

(This item is displayed on page 558 in the print version)

1 // Fig. 11.32: MouseDetails.java 2 // Testing MouseDetailsFrame. 3 import javax.swing.JFrame; 4 5 public class MouseDetails 6 { 7 public static void main( String args[] ) 8 { 9 MouseDetailsFrame mouseDetailsFrame = new MouseDetailsFrame(); 10 mouseDetailsFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); 11 mouseDetailsFrame.setSize( 400, 150 ); // set frame size 12 mouseDetailsFrame.setVisible( true ); // display frame 13 } // end main 14 } // end class MouseDetails  

Common Programming Error 11.4

If you extend an adapter class and misspell the name of the method you are overriding, your method simply becomes another method in the class. This is a logic error that is difficult to detect, since the program will call the empty version of the method inherited from the adapter class.

A user of a Java application may be on a system with a one-, two- or three-button mouse. Java provides a mechanism to distinguish among mouse buttons. Class MouseEvent inherits several methods from class InputEvent that can distinguish among mouse buttons on amultibutton mouse or can mimic amultibutton mouse with a combined keystroke and mouse-button click. Figure 11.33 shows the InputEvent methods used to distinguish among mouse-button clicks. Java assumes that every mouse contains a left mouse button. Thus, it is simple to test for a left-mouse-button click. However, users with a one- or two-button mouse must use a combination of keystrokes and mouse-button clicks at the same time to simulate the missing buttons on the mouse. In the case of a one- or two-button mouse, a Java application assumes that the center mouse button is clicked if the user holds down the Alt key and clicks the left mouse button on a two-button mouse or the only mouse button on a one-button mouse. In the case of a one-button mouse, a Java application assumes that the right mouse button is clicked if the user holds down the Meta key and clicks the mouse button.

Figure 11.33. InputEvent methods that help distinguish among left-, center- and right-mouse-button clicks.

InputEvent method

Description

isMetaDown()

Returns TRue when the user clicks the right mouse button on a mouse with two or three buttons. To simulate a right-mouse-button click on a one-button mouse, the user can hold down the Meta key on the keyboard and click the mouse button.

isAltDown()

Returns TRue when the user clicks the middle mouse button on a mouse with three buttons. To simulate a middle-mouse-button click on a one- or two-button mouse, the user can press the Alt key on the keyboard and click the only- or left-mouse button, respectively.

Line 22 of Fig. 11.31 registers a MouseListener for the MouseDetailsFrame. The event listener is an object of class MouseClickHandler, which extends MouseAdapter. This enables us to declare only method mouseClicked (lines 2945). This method first captures the coordinates where the event occurred and stores them in local variables xPos and yPos (lines 3132). Lines 3435 create a String called details containing the number of mouse clicks, which is returned by MouseEvent method getClickCount at line 35. Lines 3742 use methods isMetaDown and isAltDown to determine which mouse button the user clicked and append an appropriate String to details in each case. The resulting String is displayed in the statusBar. Class MouseDetails (Fig. 11.32) contains the main method that executes the application. Try clicking with each of your mouse's buttons repeatedly to see the click count increment.

Категории