by Dinesh Thakur Category: AWT and Applets

The MouseEvent event is used by two different interfaces:

java.awt.event.MouseListener and java.awt.event.MouseMotionListener. The first interface is designed to process events button press and mouse detection entrance and exit of a mouse over a component. The second is the processing the mouse movement.

The java.awt.event.MouseListener interface requires the implementation of severalmethods : mousePressed, mouseReleased, mouseEntered, and mouseExitedmouseClicked. To reduce the amount of required code can opt for the extension of the class java.awt.event.MouseAdapter that provides a null implementation of the above methods.

The java.awt.event.MouseMotionListener interface requires the implementation of following methods : mouseDragged and mouseMoved . As for the interface MouseListener, can opt for the extension of the class to MouseMotionAdapter reducing the amount of code required . The MouseEvent event has the following specific methods :



getClickCount ()

Returns the number of clicks associated with this event.

GetPoint ()

Returns a Point object containing the coordinates of where the activation of the mouse occurred.

getX ()

Returns the x coordinate of where they occurred Pressing the mouse.

getY ()

Returns the y coordinate of where they occurred Pressing the mouse.

isPopupTrigger ()

Checks if this event is associated with display Popup menus on this platform.

TranslatePoint (int, int)

Translates the coordinate of the event with the values specified.


import java.applet.Applet;

import java.awt.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
/* <APPLET CODE ="MouselistenerJavaAwt.class" WIDTH=300 HEIGHT=200> </APPLET> */
public class MouselistenerJavaAwt extends Applet implements MouseListener
        TextField t;
        public void init()
              t=new TextField(100);
              public void mousePressed(MouseEvent e)
                   t.setText("Mouse Pressed; # of clicks:"+e.getClickCount()+"at position"+e.getX()+","+e.getY());
                    public void mouseReleased(MouseEvent e)
                           t.setText("Mouse Released; # of clicks:"+e.getClickCount());
                            public void mouseEntered(MouseEvent e)
                                  t.setText("Mouse Entered");
                                   public void mouseExited(MouseEvent e)
                                        t.setText("Mouse exited");
                                         public void mouseClicked(MouseEvent e)
                                              t.setText("mouse clicked(# of clicks:"+e.getClickCount());

Mouselistener in Java Awt Example

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.C.A, MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.

Related Articles