The mouse button clicks from a 1, 2 and 3 button mouse can be detected
with the getModifiers()
method of the MouseEvent
class:
public
void mouseClicked (MouseEvent e) {
if (g.getModifiers () & InputEvent.BUTTON3_MASK)
!= 0)
doSomething ();
...
Here the constant BUTTON3_MASK
from the InputEvent
class provides a bit mask with which to identify whether the third
mouse button generated the event. Similarly, other buttons can be
identified with these masks:
BUTTON1_MASK
BUTTON2_MASK
ALT_MASK
META_MASK
Single button mouses can simulate a button two by holding down
the ALT key. Button three by holding down the META key. (Not all
keyboards have META keys.)
The following program shows a JPanel
subclass from the applet named MouseButtonsApplet.
An instance 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.
MouseButtonsApplet
|
import
javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/** Demonstrate how to detect which mouse button was pressed.**/
public class MouseButtonsApplet extends JApplet
{
MouseButtonPanel fMouseButtonPanel = null;
public void init () {
Container content_pane = getContentPane
();
// Create an instance of the panel
to test mouse buttons.
fMouseButtonPanel = new MouseButtonPanel
();
// And add one or more panels to the
JApplet panel.
content_pane.add (fMouseButtonPanel);
} // init
} // class MouseButtonsApplet
/** A sample JPanel class to hold components. to test mouse
buttons **/
class MouseButtonPanel extends JPanel
{
JTextArea fTextArea = null;
/** 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 so that a
long list of
// keyinputs can be seen.
JScrollPane areaScrollPane = new JScrollPane
(fTextArea);
add (areaScrollPane);
canvas.addMouseListener
( new MouseAdapter () {
public void
mouseClicked (MouseEvent e)
{
if ((e.getModifiers
() & InputEvent.BUTTON1_MASK) != 0)
saySomething ("Left button pressed", e);
if ((e.getModifiers
() & InputEvent.BUTTON2_MASK) != 0)
saySomething ("Middle button pressed",e );
if ((e.getModifiers
() & InputEvent.BUTTON3_MASK) != 0)
saySomething ("Right button3 pressed",e );
if ((e.getModifiers
() & InputEvent.ALT_MASK) != 0)
saySomething ("alt pressed" ,e);
if
((e.getModifiers () & InputEvent.META_MASK) != 0)
saySomething
("meta pressed",e );
} // mouseClicked()
} // end anonymous class
); // end method call
} // ctor
/** Indicate what mouse event occurred. **/
void saySomething (String eventDescription, MouseEvent
e) {
fTextArea.append (eventDescription
+ " on "
+
e.getComponent ().getClass ().getName ()
+
"\n");
} // saySomething
}// class MouseButtonPanel
|
Note that you can initiate a popup
menu without explicitly testing for particular buttons. See
the Popup Menu section for more about
this.
References & Web Resources
Latest update: Dec. 6, 200
|