Books
in black and white
Main menu
Share a book About us Home
Books
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics
Ads

More Java Pitfalls Share Reactor - Daconta M,C.

Daconta M,C. More Java Pitfalls Share Reactor - Wiley publishing, 2003. - 476 p.
ISBN: 0-471-23751-5
Download (direct link): morejavapitfallssharereactor2003.pdf
Previous << 1 .. 29 30 31 32 33 34 < 35 > 36 37 38 39 40 41 .. 166 >> Next

In mouseClicked(), isPopupTrigger? false
Here is a run of BadRightMouseButton.java on Mac OSX with a single-button mouse. When the program was executed, the single mouse button was clicked (which maps to button 1), then the Ctrl key was held and the mouse button clicked, then the special "apple" key was held and the mouse button clicked.
>>>java BadRightMouseButton (on MacOSX with a single mouse button)
Button 1 clicked.
isControlDown? false isMetaDown? false isAltDown? false isShiftDown? false isAltGraphDown? false
In mouseClicked(), isPopupTrigger? false
Button 2 clicked.
isControlDown? true isMetaDown? false isAltDown? true isShiftDown? false isAltGraphDown? false
In mouseClicked(), isPopupTrigger? false
Button 3 clicked.
isControlDown? false isMetaDown? true isAltDown? false isShiftDown? false isAltGraphDown? false
In mouseClicked(), isPopupTrigger? false
There are two problems you should notice when examining the results from both operating systems besides the fact that they are not consistent. First, there is no clear indication of a right mouse button click even though we know that the Windows mouse clearly has a right and left mouse button. Instead of "sides" of the mouse, we have the ability in the InputEvent class (which is the parent class of MouseEvent) to check which button was clicked: button 1, 2, or 3. Unfortunately, there is no way to correlate a button with a side for a cross-platform application. The second problem is that the call to isPopupTrigger() has always returned false when we know that a right-button mouse click is the trigger on Windows for a popup and the Ctrl-mouse click combination is the trigger on the Mac. Listing 10.2, GoodRightMouseButton.java, solves both of these problems.
Mouse Button Portability 85
01 /* GoodRightMouseButton.java */
02 import java.awt.event.*;
03 import java.awt.*;
04 import javax.swing.*;
05
06 public class GoodRightMouseButton extends JFrame implements 2
MouseListener
07 {
08 public GoodRightMouseButton()
09 {
// ... constructor identical to Listing #10.1
28 }
29
30 public static void main(String [] args)
31 {
32 try
33 {
34 GoodRightMouseButton win = new GoodRightMouseButton();
35
36 } catch (Throwable t)
37 {
38 t.printStackTrace();
39 }
40 }
41
42 public void mouseClicked(MouseEvent e)
43 {
// ... getModifiers() code Identical to listing 10.1 ...
68
69 // is this a popup Trigger?
70 System.out.println("In mouseClicked(), isPopupTrigger? " + 2
e.isPopupTrigger());
71
72 // Use SwingUtilities to disambiguate
73 boolean lb = SwingUtilities.isLeftMouseButton(e);
74 boolean mb = SwingUtilities.isMiddleMouseButton(e);
75 boolean rb = SwingUtilities.isRightMouseButton(e);
76
77 System.out.println("Left button? " + lb);
78 System.out.println("Middle button? " + mb);
79 System.out.println("Right button? " + rb);
80 }
81
82 public void mousePressed(MouseEvent e)
Listing 10.2 GoodRightMouseButton.java (continued)
86 Item 10
83 {
84 // is this a popup Trigger?
85 System.out.println("In mousePressed(), isPopupTrigger? + 2
e.isPopupTrigger());
86 }
87 public void mouseReleased(MouseEvent e)
88 {
89 // is this a Popup Trigger?
90 System.out.println("In mouseReleased(), isPopupTrigger? " + 2
e.isPopupTrigger());
91 }
92
93 public void mouseEntered(MouseEvent e)
94 { }
95 public void mouseExited(MouseEvent e)
96 { }
97 }
98
Listing 10.2 (continued)
Here is a run of GoodRightMouseButton on Windows. When executing the program, the right mouse button was clicked, followed by the left mouse button and then the mouse wheel. This produced the following println statements:
>>>java GoodRightMouseButton (on Windows NT with 2 button mouse with mousewheel)
In mousePressed(), isPopupTrigger? false
In mouseReleased(), isPopupTrigger? true
Button 3 clicked.
isControlDown? false
isMetaDown? true
isAltDown? false
isShiftDown? false
isAltGraphDown? false
In mouseClicked(), isPopupTrigger? false Left button? false Middle button? false Right button? true
In mousePressed(), isPopupTrigger? false In mouseReleased(), isPopupTrigger? false Button 1 clicked.
isControlDown? false isMetaDown? false isAltDown? false isShiftDown? false isAltGraphDown? false
Mouse Button Portability 87
In mouseClicked(), isPopupTrigger? false Left button? true Middle button? false Right button? false
In mousePressed(), isPopupTrigger? false
In mouseReleased(), isPopupTrigger? false
Button 2 clicked.
isControlDown? false
isMetaDown? false
isAltDown? true
isShiftDown? false
isAltGraphDown? false
In mouseClicked(), isPopupTrigger? false Left button? false Middle button? true Right button? false
Here is a run of GoodRightMouseButton.java on Mac OSX. When the program was executed, the single mouse button was clicked, then the Ctrl key was held and the mouse button clicked, then the special "apple" key was held and the mouse button clicked.
Previous << 1 .. 29 30 31 32 33 34 < 35 > 36 37 38 39 40 41 .. 166 >> Next