View RSS Feed

Java Swing

How to Write a Mouse Listener

Rate this Entry
by , 04-26-2012 at 09:38 PM (879 Views)
To do interaction with a component, mouse is used by the user and this is notified by the mouse events. Usually mouse events take place when cursor gets entered or exit from onscreen area of the component and when button of mouse is pressed or released.

To track down the motion of cursor usually involve more system;s overheads as compared to tracking of the other events. This is the reason why mouse motion events have been placed separately in Mouse motion, listener type.

Mouse wheel listener shall be registered in case one needs to track down the mouse wheel events.

If both mouse motion events and moust events are required to be detected then class MouseInputAdapter shall be used. MouseInputAdapter is implemented by this class, which is an easier interface for implementation of the MouseMotionListener and MouseListener interfaces. However, interface MouseWheelListener is not implemented by the MouseInputListener interface.
Alternatively, correspondent MouseAdapter class shall be used for implementation of interfaces like, MouseMotionListener, MouseWheelListener & MouseListener.

Mouse listener is being shown in the given example. At window’s top, there is present a blank area which is basically implemented by the help of class BlankArea. Events are listened by the mouse listener, both at the container and the BlankArea. Everytime a mouse-event takes place, blank area gets filled up with a descriptive message.

Submit "How to Write a Mouse Listener" to Facebook Submit "How to Write a Mouse Listener" to Digg Submit "How to Write a Mouse Listener" to Submit "How to Write a Mouse Listener" to StumbleUpon Submit "How to Write a Mouse Listener" to Google