Owl Teach Yourself Java 2 in 21 Days, Second Edition
HOME / ORDER THE BOOK / OTHER BOOKS / THANKS / PREVIOUS DAY / NEXT DAY
ROGERS CADENHEAD

Day 11: Responding to User Input

Notes and Corrections

  • None.

Source Files

Reader Questions

  • Question: Is there a way of differentiating between the buttons on a mouseClicked() event?
    Answer: You can, using a feature of mouse events that isn't covered in the book -- primarily because right and middle mouse buttons are platform specific features that aren't available on all systems where Java programs run.
         All mouse events send a MouseEvent object to their event-handling methods. Call the getModifiers() method of the object to receive an integer value that indicates which mouse button generated the event.
        Check the value against three class variables. It equals  MouseEvent.BUTTON1_MASK if the left button was clicked,  MouseEvent.BUTTON2_MASK if the middle button was clicked, and MouseEvent.BUTTON3_MASK if the right button was clicked. See MouseTest.java and MouseTest.class for an example that implements this.
         For more information, see the documentation for the MouseEvent class, which is part of the online Java 2 SDK 1.3 class library documentation.

Activities

HOME / ORDER THE BOOK / OTHER BOOKS / THANKS / PREVIOUS DAY / NEXT DAY

Workbench | Books | RSS Feed | RSS Spec | Copyright 2017 World Readable