MouseListener
Previous    Next    Home    Source Code    Package

Notes
  1. In this example we combine the ability to listen to mouse motion events with the ability to listen to other mouse events such as clicking. Thus we implement both the MouseMotionListener and MouseListener interfaces. This requires implementing mouseMoved, mousePressed, mouseDragged, mouseClicked, mouseEntered, mouseExited, and mouseReleased even though we only need to implement the first two of these. Things are getting out of hand!
  2. This example used implements a MousePad class, as a Panel, which gets "bolted" into the applet.
  3. There is a problem with this applet. The JTextField can be edited by the user so something other than a number may be put there. If there is a number there, clicking in the applet will increase it by 1. But if there is not a number there the applet does not know what to do and does nothing until a number is put there again.
  4. Observe that Integer.parseInt is used to convert a String to a number so 1 can be added to it. Then String.valueOf is used to convert back to a String that the textfield is set to.