Home : Course Map : Chapter 12 : Java :
Mouse Buttons
JavaTech
Course Map
Chapter 12

Printing
  Demo 1
Cursor Icons
  Demo 2
MouseButtons
  Demo 3
PopupMenu
  Demo 4
Keystrokes
  Demo 5
Audio
  Demo 6
Timing & Speed
  Demo 7
Toolkit
  Demo 8
AppletContext
  Demo 9
Exercises

    Supplements
Java Beans
More APIs
Java & Browsers
  Demo 1
     About JavaTech
     Codes List
     Exercises
     Feedback
     References
     Resources
     Tips
     Topic Index
     Course Guide
     What's New

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

 

              Tech
Tech APIs
Exercises

           Physics
Math/Science APIs
JAIDA Program
  Demo 1
Demo 1 Discussion
Exercises

  Part I Part II Part III
Java Core 1  2  3  4  5  6  7  8  9  10  11  12 13 14 15 16 17
18 19 20
21
22 23 24
Supplements

1  2  3  4  5  6  7  8  9  10  11  12

Tech 1  2  3  4  5  6  7  8  9  10  11  12
Physics 1  2  3  4  5  6  7  8  9  10  11  12

Java is a trademark of Sun Microsystems, Inc.