Java Programming | MouseMotionListener in Java | Java Swing Tutorial | Java MouseMotionListener

preview_player
Показать описание
Java MouseMotionListener Interface
Methods of MouseMotionListener interface.
The signature of 2 methods found in MouseMotionListener interface

#MouseMotionListener #Java #JavaSwingtutorial #Javatutorialforbeginners #JavaProgramming #JavaGUI

The listener interface for receiving mouse motion events on a component. (For clicks and other mouse events, use the MouseListener.)

The class that is interested in processing a mouse motion event either implements this interface (and all the methods it contains) or extends the abstract MouseMotionAdapter class (overriding only the methods of interest).

The listener object created from that class is then registered with a component using the component's addMouseMotionListener method. A mouse motion event is generated when the mouse is moved or dragged. (Many such events will be generated). When a mouse motion event occurs, the relevant method in the listener object is invoked, and the MouseEvent is passed to it.

How to Write a Mouse-Motion Listener

Mouse-motion events notify when the user uses the mouse (or a similar input device) to move the onscreen cursor. For information on listening for other kinds of mouse events, such as clicks, see How to Write a Mouse Listener. For information on listening for mouse-wheel events, see How to Write a Mouse Wheel Listener.

If an application requires the detection of both mouse events and mouse-motion events, use the MouseInputAdapter class, which implements the MouseInputListener a convenient interface that implements both the MouseListener and MouseMotionListener interfaces.

Alternatively, use the corresponding MouseAdapter AWT class, which implements the MouseMotionListener interface, to create a MouseMotionEvent and override the methods for the specific events.

The following demo code contains a mouse-motion listener. This demo is exactly the same as the demo described in the How to Write a Mouse Listener section, except for substituting the MouseMotionListener interface for the MouseListener interface. Additionally, MouseMotionEventDemo implements the mouseDragged and mouseMoved methods instead of the mouse listener methods, and displays coordinates instead of numbers of clicks.

MouseListener and MouseMotionListener in Java
are of two types. MouseListener handles the events when the mouse is not in motion. While MouseMotionListener
handles the events when mouse is in motion.
There are five types of events that MouseListener can generate. There are five abstract functions that represent these five events. The abstract functions are :


void mouseReleased(MouseEvent e) : Mouse key is released
void mouseClicked(MouseEvent e) : Mouse key is pressed/released
void mouseExited(MouseEvent e) : Mouse exited the component
void mouseEntered(MouseEvent e) : Mouse entered the component
void mousepressed(MouseEvent e) : Mouse key is pressed

There are two types of events that MouseMotionListener can generate. There are two abstract functions that represent these five events. The abstract functions are :


void mouseDragged(MouseEvent e) : Invoked when a mouse button is pressed in the component and dragged. Events are passed until the user releases the mouse button.
void mouseMoved(MouseEvent e) : invoked when the mouse cursor is moved from one point to another within the component, without pressing any mouse buttons.

SWING - MouseMotionListener Interface
The interface MouseMotionListener is used for receiving mouse motion events on a component. The class that processes mouse motion events needs to implements this interface.

Рекомендации по теме
Комментарии
Автор

very well explained mousemotionlistener tutorial keep it up

ImranKhan-wzlb