Java Reference
In-Depth Information
Common Programming Error 12.3
If you extend an adapter class and misspell the name of the method you're overriding, and
you do not declare the method with @Override , 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. Class MouseEvent inherits several methods from class InputEvent that can distin-
guish among mouse buttons on a multibutton mouse or can mimic a multibutton mouse
with a combined keystroke and mouse-button click. Figure 12.33 shows the InputEvent
methods used to distinguish among mouse-button clicks. Java assumes that every mouse
contains a left mouse button. Thus, it's simple to test for a left-mouse-button click. How-
ever, 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 (sometimes called the Command key or the “Apple” key on a
Mac) and clicks the mouse button.
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 and click the only or left mouse button, respectively.
Fig. 12.33 | InputEvent methods that help determine whether the right or center mouse
button was clicked.
Line 21 of Fig. 12.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 28-45). This method first captures
the coordinates where the event occurred and stores them in local variables xPos and yPos
(lines 31-32). Lines 34-35 create a String called details containing the number of con-
secutive mouse clicks, which is returned by MouseEvent method getClickCount at line 35.
Lines 37-42 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. 12.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.
 
Search WWH ::




Custom Search