Java mouse click double click

Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here 🙂

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:

Читайте также:  Enumerate python 3 словари

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

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:

Читайте также:  Send string to function php

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

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 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.

Читайте также:  Rock Paper Scissor

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:

Источник

Оцените статью