1. AWT MouseListener & MouseMotionListener
The MouseListener and MouseMotionListener are listener interfaces for picking up the mouse actions. MouseListener is to, track the mouse events–Enter, Exit, Press, Release and Click. MouseMotionListener is to capture the mouse move as well as mouse drag. Note, drag is nothing but moving the mouse with one of its buttons in pressed state. For example, to move a window from one area to other area on the desktop screen we end up in dragging the mouse with the left button pressed down on the window title.
In this example, we will explore both the listeners and how it captures the mouse events. Let us start Java AWT Mouse Example.
2. Mouse Buttons & AWT Button Constants
In 80s, usually mouse came with only two buttons. One is for left click and other one is for right click. Now as days mouse comes with three buttons and a wheel in the middle. The wheel allows scrolling the contents, and also it allows click. Below is the picture of the modern days mouse:

The above picture has three mouse buttons. Java AWT will identify these buttons with constants BUTTON1, BUTTON2 and BUTTON3. These constants are sequential from left to right. Means, in the picture we can map the buttons with Java AWT constants as follows:
- Mouse Button 1 => Left Mouse Button => BUTTON1
- Mouse Button 2 => Right Mouse Button => BUTTON3
- Mouse Button 3 => Middle Mouse Button => BUTTON2
Note, you can do a middle click by pressing and releasing the wheel. The wheel is multi purpose as you can do scroll and click.
3. AWT MouseListener Events
AWT MouseListener is useful to track the mouse events. Have a look at the below picture:

The MouseListener will watch for Six mouse events. They are:
- Mouse Pressed: The event is received when the user pressed any of the possible three mouse buttons.
- Mouse Released: When the user released the pressed mouse button, this event is thrown.
- Mouse Enter: The component registered with the listener will receive this event when mouse cursor enters it.
- Mouse Exit: When the cursor goes off the component, AWT reports this exit event.
- Mouse Clicked: When mouse is pressed and released at the same location (x & y), AWT will produce Mouse Clicked event.
Say for example, we want to track mouse enter and exit on the TextArea component. Then, to achieve it, we will register the TextArea component with the MouseListener. Now TextArea component will send the mouse events to the MouseListener handler function.
4. AWT MouseMotionListener Events
MouseMotionListener will look for the mouse move and mouse drag events. It captures the mouse path in terms of x and y coordinate values. Have a look at the below picture:

The picture shows how the MouseMotionListener tracks the mouse motion path. The event will be made at periotic interval quick enough to capture the motion path solidly. However, moving the mouse so fast will not produce enough events and listener will lose intermediate x & y values. Mouse Drag is same as Mouse Move with an exception that at least one button should in pressed state while mouse is moving. Mouse Drag is vital to carry out Drag & Drop feature.
5. Learn Mouse Press, Release, Click & Double-Click
Mouse Click is a combination of pressing a specific mouse button and releasing it. The double-click is a combination of two mouse clicks. Have a look at the below picture:

The top portion shows the click event of the mouse. Java AWT treats a mouse button press and release as a click when the press & release happens at the same coordinate location.
For a double click, the click events (two clicks) should happen within a specific time limit. In the above picture (bottom portion), the first two clicks happened for a long duration and hence they are treated as two separate mouse clicks. The next four clicks happened so quickly and hence AWT considered it as two Double-Click events. The above picture notes this as with two Green arrow marks. In windows OS, we can configure double-click time interval via control panel -> mouse settings.
6. About the MouseListener Example
Now we have basic information about the mouse events. The below picture shows the example we want to create:

AWT Frame Window houses a AWT Panel in the middle. This is the panel which will track the mouse events in our example. In the bottom of the Frame, we will have a AWT Label Control to report the mouse events. Now, let us proceed with the example.
7. Frame Window
In the program entry, we create an instance of the AWTMouseExample
which we will derive from the Frame window. Next, we set this Frame Window visible to the users. Below is the code:
1 2 3 4 5 6 7 |
public class MainEntryAwt { public static void main(String[] args) { //Sample 01: Make the FrameWindow Visible AWTMouseExample fw = new AWTMouseExample("JSplitPane Example"); fw.setVisible(true); } } |
In the below code snippet, we set title, size, and location of the Frame Window. Also, note the class AWTMouseExample
extends from the AWT Frame
class. Our frame window class registers with the WindowListener to implement closing action.
1 2 3 4 5 6 7 |
public class AWTMouseExample extends Frame implements WindowListener { public AWTMouseExample(String FrameTitle) { //Sample 02: Set Frame Window Properties super(FrameTitle); setSize(400, 300); setLocation(100, 100); addWindowListener(this); |
8. Capture MouseEvent via MouseListener
We create a class MouseTestPanel
which extends from the AWT Panel
. This is a container and also a component. Since we will add this to the Frame Window AWTMouseExample
, the Frame sees this as a component. We will make this panel listen for the Mouse Events. Now, we implement the
MouseListener for our Panel class.
8.1 Constructor of MouseTestPanel
In the constructor, we set the status Label which we will pass from the Frame Window. The Label is not yet constructed, and we do that while we go back to coding of AWTMouseExample
which we left halfway. Note, we also set preferred size for the Panel.
1 2 3 4 5 6 7 8 9 10 11 |
//Sample 03: Create a Panel & Implement MouseListeners public class MouseTestPanel extends Panel implements MouseListener{ Label Status; public MouseTestPanel(Label status) { super(); this.Status = status; setPreferredSize(new Dimension(300, 200)); } |
8.2 The mouseClicked Handler of MouseListener
Java AWT calls
mouseClicked event handler with
MouseEvent as an argument when the user clicks any of the mouse button. We can use the
getModifiers method on the MouseEvent
to get the binary mask as an integer value. Then, we can use the bitwise and
operation to know which button is clicked by the user. The constants are from
InputEvent class. For example, to check middle mouse button click we can perform the bitwise AND (&) on the constant
BUTTON2. The same goes for other buttons.
Methods
getX and
getY exposed by the MouseEvent
tells us the coordinate location in which user clicked the mouse button. In the below code, we set the status text which shows which mouse button the user clicked and at what location the click originates. Below is the event handler:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
@Override public void mouseClicked(MouseEvent e) { //Sample 04: Check which Mouse Button Clicked String out = ""; int ButtonClickMask = e.getModifiers(); if ((ButtonClickMask & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK) out += "Left Mouse Clicked "; if ((ButtonClickMask & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK) out += "Middle Mouse Clicked "; if ((ButtonClickMask & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK) out += "Right Mouse Clicked "; //Sample 05: Get the Location out += "@ " + e.getX() + "," + e.getY(); Status.setText(out); } |
8.3 Other MouseListener Handlers – Press, Release, Enter & Exit
For all other event handlers, we still get MouseEvent object. This means, we can tell which mouse button and coordinate location tied to the MouseEvent passed in for all these events. In the below code, we set the status text stating the mouse action. Note, we will register our Panel control with the MouseListener. So, the below code tracks mouse interaction with the Panel. For example, we will get the message, “Mouse Entered” when the mouse cursor enters the Panel. The same goes for other events.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
//Sample 06: Other MouseListener Events @Override public void mouseEntered(MouseEvent e) { Status.setText("Mouse Entered"); } @Override public void mouseExited(MouseEvent e) { Status.setText("Mouse Left"); } @Override public void mousePressed(MouseEvent e) { Status.setText("Mouse Button Pressed"); } @Override public void mouseReleased(MouseEvent e) { Status.setText("Mouse Button Released"); } |
9. Implementing MouseMotionListener
In this section, we will implement the handlers to track the mouse movement. This includes both move and drag.
9.1 Implement MouseMotionListener
We implement
MouseMotionListener to tract the mouse movement as well as mouse drag. The below code shows our MouseTestPanel
implements this Interface also:
1 2 3 4 5 |
//Sample 03: Create a Panel & Implement MouseListeners //Sample 07: Implement MouseMotionListener public class MouseTestPanel extends Panel implements MouseListener, MouseMotionListener { |
9.2 MouseMotionListener Handlers – Move & Drag
As already told, drag is a mouse move with a mouse button kept in pressed state. The drag ends when the button is released. In the below code, we track both mouse move and mouse drag via the handler functions mouseMoved and mouseDragged.
1 2 3 4 5 6 7 8 9 10 11 12 |
//Sample 08: Track Mouse Movement/Drag @Override public void mouseDragged(MouseEvent arg0) { Status.setText("Mouse Draggged. (" + arg0.getX() + "," + arg0.getY() + ")"); } @Override public void mouseMoved(MouseEvent arg0) { Status.setText("Mouse Moved. (" + arg0.getX() + "," + arg0.getY() + ")"); } |
10. AWT Panel Listening For Mouse Actions
Now, we will go back to our Frame class which was left half-way in Section 7. By this time, our test panel is ready to track all the mouse events. Here, we will make use of it.
10.1 Add AWT Component to Frame Window
Here, we create the status label & mouse tracking test panel and then add them to the AWT Frame. We add the Gray colour panel in the middle of the Frame and status label in the South of the frame window. This completes the UI.
1 2 3 4 5 6 |
//Sample 09: Create Mouse Test Panel Label StatusLabel = new Label(); MouseTestPanel panel = new MouseTestPanel(StatusLabel); panel.setBackground(Color.gray); add(panel); add(BorderLayout.SOUTH, StatusLabel); |
10.2 Register with MouseListener & MouseMotionListener
Our frame window hosts only two components. One is a Panel and other one is a Label. Label will tell the status and Panel will look for the mouse events. If you look at the code snippet in the past section, we pass the label to the MouseTestPanel
. This way, the MouseTestPanel
will have access to the Label taken care of by the AWT Frame. The below code registers the Panel with both the mouse listeners by calling the methods
addMouseListener and
addMouseMotionListener. Note, we pass the same Panel instance as it implements
MouseListener and
MouseMotionListener.
1 2 3 |
//Sample 10: Register with Mouse Move panel.addMouseListener(panel); panel.addMouseMotionListener(panel); |
11. Code Reference – MouseListener & MouseMotionListener
11.1 MainEntryAWT.java
1 2 3 4 5 6 7 8 9 10 11 |
package AwtDemoPkg; import java.awt.Frame; public class MainEntryAwt { public static void main(String[] args) { //Sample 01: Make the FrameWindow Visible AWTMouseExample fw = new AWTMouseExample("JSplitPane Example"); fw.setVisible(true); } } |
11.2 AWTMouseExample.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
package AwtDemoPkg; import java.awt.BorderLayout; import java.awt.Color; import java.awt.FlowLayout; import java.awt.Frame; import java.awt.Label; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; public class AWTMouseExample extends Frame implements WindowListener { public AWTMouseExample(String FrameTitle) { //Sample 02: Set Frame Window Properties super(FrameTitle); setSize(400, 300); setLocation(100, 100); addWindowListener(this); //Sample 09: Create Mouse Test Panel Label StatusLabel = new Label(); MouseTestPanel panel = new MouseTestPanel(StatusLabel); panel.setBackground(Color.gray); add(panel); add(BorderLayout.SOUTH, StatusLabel); //Sample 10: Register with Mouse Move panel.addMouseListener(panel); panel.addMouseMotionListener(panel); } public void windowOpened(WindowEvent e) { } public void windowClosing(WindowEvent e) { this.dispose(); } public void windowClosed(WindowEvent e) { } public void windowIconified(WindowEvent e) { } public void windowDeiconified(WindowEvent e) { } public void windowActivated(WindowEvent e) { } public void windowDeactivated(WindowEvent e) { } } |
11.3 MouseTestPanel.Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
package AwtDemoPkg; import java.awt.Dimension; import java.awt.Label; import java.awt.Panel; import java.awt.event.InputEvent; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; //Sample 03: Create a Panel & Implement MouseListeners //Sample 07: Implement MouseMotionListener public class MouseTestPanel extends Panel implements MouseListener, MouseMotionListener { Label Status; public MouseTestPanel(Label status) { super(); this.Status = status; setPreferredSize(new Dimension(300, 200)); } @Override public void mouseClicked(MouseEvent e) { //Sample 04: Check which Mouse Button Clicked String out = ""; int ButtonClickMask = e.getModifiers(); if ( (ButtonClickMask & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK ) out += "Left Mouse Clicked "; if ( (ButtonClickMask & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK ) out += "Middle Mouse Clicked "; if ( (ButtonClickMask & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK ) out += "Right Mouse Clicked "; //Sample 05: Get the Location out += "@ " + e.getX() + "," + e.getY(); Status.setText(out); } //Sample 06: Other MouseListener Events @Override public void mouseEntered(MouseEvent e) { Status.setText("Mouse Entered"); } @Override public void mouseExited(MouseEvent e) { Status.setText("Mouse Left"); } @Override public void mousePressed(MouseEvent e) { Status.setText("Mouse Button Pressed"); } @Override public void mouseReleased(MouseEvent e) { Status.setText("Mouse Button Released"); } //Sample 08: Track Mouse Movement/Drag @Override public void mouseDragged(MouseEvent arg0) { Status.setText("Mouse Draggged. (" + arg0.getX() + "," + arg0.getY() + ")"); } @Override public void mouseMoved(MouseEvent arg0) { Status.setText("Mouse Moved. (" + arg0.getX() + "," + arg0.getY() + ")"); } } |
12. AWT Mouse Events Example – Youtube Demo
Categories: AWT
Tags: getModifiers, mouseDragged, MouseEvent, MouseListener, MouseMotionListener, mouseMoved