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 .. 28 29 30 31 32 33 < 34 > 35 36 37 38 39 40 .. 166 >> Next

Table 10.1 Mouse Event Interfaces
INTERFACE METHOD DESCRIPTION
MouseListener mouseClicked Invoked when a mouse is clicked on a component
mousePressed Invoked when a mouse is pressed
mouseReleased Invoked when a mouse is released
mouseEntered Invoked when a mouse enters a component's bounds
mouseExited Invoked when a mouse exits a component's bounds
MouseMotionListener mouseDragged Invoked when a mouse button is pressed on a component and then dragged
mouseMoved Invoked when a mouse is moved around a component
MouseWheelListener mouseWheelMoved Invoked when the mouse wheel is rotated
Mouse Button Portability 81
l&Bad Right Moute Button BED
Ritlht Mouse Click hurt'.
0OG K ill Right Mouse Button

Right Mouse Click here.
Figure 10.1 BadRightMouseButton on Windows (top) and Mac OS X (bottom).
A common problem is an application that needs to process clicks from a right mouse button across platforms. For example, say we had an application that captured right mouse clicks to both display a context-sensitive popup menu and change the mode of a tool from select, on the left mouse click, to select and execute for a right mouse click. Figure 10.1 displays the user interface of a simple application to capture these two activities of a right mouse click. Figure 10.1 shows the application run on both Windows NT and Mac OSX, since our Java application supports both operating systems.
Listing 10.1 displays the source code for BadRightMouseButton.java.
01 /* BadRightMouseButton.java */
02 import java.awt.event.*;
03 import java.awt.*;
04 import javax.swing.*;
05
06 public class BadRightMouseButton extends JFrame implements
MouseListener
07 {
08 public BadRightMouseButton()
09 {
10 super("Bad Right Mouse Button");
11
12 JLabel l = new JLabel("Right Mouse Click here.");
13 getContentPane().add("Center", l);
14
15 addMouseListener(this);
16
17 setSize(400,200);
18 setLocation(100,100);
19 setVisible(true);
20 addWindowListener(new WindowAdapter()
21 {
22 public void windowClosing(WindowEvent evt)
Listing 10.1 BadRightMouseButton.java (continued)
82 Item 10
23: {
24: System.exit(l);
25: }
26: });
27:
28: }
29:
30: public static void main(String [] args)
31: {
32 : try
33 : {
34: BadRightMouseButton win = new BadRightMouseButton();
35:
36: } catch (Throwable t)
37 : {
38 : t.printStackTrace();
39 : }
40 : }
41:
42: public void mouseClicked(MouseEvent e)
43: {
44: int modifiers = e.getModifiers();
45: if ((modifiers & InputEvent.BUTTON1_MASK) == 2
InputEvent BUTTON1_MASK)
46: System.out.println("Button 1 clicked.");
47:
48: if ((modifiers & InputEvent.BUTTON2_MASK) ==
InputEvent BUTTON2_MASK)
49: System.out.println("Button 2 clicked.");
50:
51: if ((modifiers & InputEvent.BUTTON3_MASK) ==
InputEvent BUTTON3_MASK) )
52: System.out.println("Button 3 clicked.");
53:
54: // modifier keys
55: System.out.println("isControlDown? " + e.isControlDown()) ;
56: System.out.println("isMetaDown? " + e.isMetaDown());
57: System.out.println("isAltDown? " + e.isAltDown());
58: System.out.println("isShiftDown? " + e.isShiftDown());
59: System.out.println("isAltGraphDown? " + e.isAltGraphDown());
60:
61: /* 1.4 methods
62: int buttonNumber = e.getButton();
63: System.out.println("Button # is : " + buttonNumber);
64:
65: int mods = e.getModifiersEx();
66: System.out.println("Modifiers: " + 2
InputEvent getModifiersExText(mods));
67: */
Listing 10.1 (continued)
Mouse Button Portability 83
68
69 // is this a popup Trigger?
70 System.out.println("In mouseClicked(), isPopupTrigger? " + 2
e.isPopupTrigger());
71 72 }
73 public void mousePressed(MouseEvent e)
74 { }
75 public void mouseReleased(MouseEvent e)
76 { }
77 public void mouseEntered(MouseEvent e)
78 { }
79 public void mouseExited(MouseEvent e)
80 { }
81 }
Listing 10.1 (continued)
JDK 1.4 has added some new methods as demonstrated (but commented out) in lines 62 and 65. These are additional convenience methods that enable you to eliminate the need for ANDing the modifier integer with the constant flags (lines 45, 48, and 51).
Here is a run of BadRightMouseButton on Windows with a two-button mouse with a mouse wheel. When the program was executed, the right mouse button was clicked, followed by the left mouse button and then the mouse wheel. This produced the following println statements:
>>>java BadRightMouseButton (on Windows NT with 2 button mouse with mousewheel)
Button 3 clicked. isControlDown? false isMetaDown? true isAltDown? false isShiftDown? false isAltGraphDown? false
In mouseClicked(), isPopupTrigger? false
Button 1 clicked.
isControlDown? false isMetaDown? false isAltDown? false isShiftDown? false isAltGraphDown? false
In mouseClicked(), isPopupTrigger? false
Button 2 clicked.
isControlDown? false isMetaDown? false isAltDown? true
84 Item 10
isShiftDown? false isAltGraphDown? false
Previous << 1 .. 28 29 30 31 32 33 < 34 > 35 36 37 38 39 40 .. 166 >> Next