Tracking Mouse Events on a JPanel

The MouseTracker application (Figs. 12.2812.29) demonstrates the MouseListener and MouseMotionListener interface methods. The event-handler class (lines 36–97 of Fig. 12.28) implements both interfaces. You must declare all seven methods from these two interfaces when your class implements them both. Each mouse event in this example displays a String in the JLabel called statusBar that is attached to the bottom of the window.

 1   // Fig. 12.28: MouseTrackerFrame.java 2   // Mouse event handling. 3   import java.awt.Color; 4   import java.awt.BorderLayout; 5   import java.awt.event.MouseListener; 6   import java.awt.event.MouseMotionListener; 7   import java.awt.event.MouseEvent; ...

Get Java™ How To Program (Early Objects), Tenth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.