Java Reference
In-Depth Information
The constant BUTTON3 - MASK from the InputEvent class provides a bit mask
with which to identify whether the third mouse button generated the event. Sim-
ilarly, other buttons and button key combinations can be identified with these
masks:
BUTTON1 - MASK
BUTTON2 - MASK
ALT - MASK
META - MASK
The latter two constants are used to determine if the keyboard ALT or META keys
were held down during the mouse click event. (Not all keyboards have META
keys.) The following code snippet shows a JPanel subclass from the applet
named MouseButtonsApplet .Aninstance of a MouseAdapter subclass
created via the inner class technique is added to the panel's MouseListener
list. The adapter's mouseClicked() method uses getModifiers() to obtain
the identity of the buttons that initiated the click and sends a message to a text
area accordingly.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MouseButtonsApplet extends JApplet
{
... Code to add a MouseButtonPanel instance to the applet ...
}
class MouseButtonPanel extends JPanel
{
JTextArea fTextArea;
/** Build the panel interface and a mouse listener. **/
MouseButtonPanel () {
setLayout (new GridLayout (2,1));
JPanel canvas = new JPanel ();
add (canvas);
canvas.setBackground (Color.red);
fTextArea = new JTextArea ();
fTextArea.setEditable (false);
// Add to a scroll pane sothatalonglistofkeyinputs can be seen.
JScrollPane area - scroll - pane = new JScrollPane (fTextArea);
add (area - scroll - pane);
 
Search WWH ::




Custom Search