Mouse-Press-Mouse-Release-Click-Double-Click
AWT

AWT MouseListener & MouseMotionListener

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:

Mouse Buttons
Mouse Buttons

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:

AWT MouseListener
AWT MouseListener

The MouseListener will watch for Six mouse events. They are:

  1. Mouse Pressed: The event is received when the user pressed any of the possible three mouse buttons.
  2. Mouse Released: When the user released the pressed mouse button, this event is thrown.
  3. Mouse Enter: The component registered with the listener will receive this event when mouse cursor enters it.
  4. Mouse Exit: When the cursor goes off the component, AWT reports this exit event.
  5. 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:

AWT MouseMotionListener
AWT MouseMotionListener

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:

Mouse-Press-Mouse-Release-Click-Double-Click
Mouse-Press-Mouse-Release-Click-Double-Click

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 Mouse Example
AWT Mouse Example

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:

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.

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.

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:

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.

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:

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.

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.

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.

11. Code Reference – MouseListener & MouseMotionListener

11.1 MainEntryAWT.java

11.2 AWTMouseExample.java

11.3 MouseTestPanel.Java

12. AWT Mouse Events Example – Youtube Demo

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.