Skip to main content

JAVA MouseListener, MouseMotionListener

Java MouseListener Interface

The Java MouseListener is notified whenever you change the state of mouse. It is notified against MouseEvent. The MouseListener interface is found in java.awt.event package. It has five methods.

Methods of MouseListener interface

The signature of 5 methods found in MouseListener interface are given below:

  1. public abstract void mouseClicked(MouseEvent e);  
  2. public abstract void mouseEntered(MouseEvent e);  
  3. public abstract void mouseExited(MouseEvent e);  
  4. public abstract void mousePressed(MouseEvent e);  
  5. public abstract void mouseReleased(MouseEvent e);  

Java MouseListener Example

  1. import java.awt.*;  
  2. import java.awt.event.*;  
  3. public class MouseListenerExample extends Frame implements MouseListener{  
  4.     Label l;  
  5.     MouseListenerExample(){  
  6.         addMouseListener(this);  
  7.           
  8.         l=new Label();  
  9.         l.setBounds(20,50,100,20);  
  10.         add(l);  
  11.         setSize(300,300);  
  12.         setLayout(null);  
  13.         setVisible(true);  
  14.     }  
  15.     public void mouseClicked(MouseEvent e) {  
  16.         l.setText("Mouse Clicked");  
  17.     }  
  18.     public void mouseEntered(MouseEvent e) {  
  19.         l.setText("Mouse Entered");  
  20.     }  
  21.     public void mouseExited(MouseEvent e) {  
  22.         l.setText("Mouse Exited");  
  23.     }  
  24.     public void mousePressed(MouseEvent e) {  
  25.         l.setText("Mouse Pressed");  
  26.     }  
  27.     public void mouseReleased(MouseEvent e) {  
  28.         l.setText("Mouse Released");  
  29.     }  
  30. public static void main(String[] args) {  
  31.     new MouseListenerExample();  
  32. }  
  33. }  

Output:

java awt mouselistener example 1

Java MouseListener Example 2

  1. import java.awt.*;  
  2. import java.awt.event.*;  
  3. public class MouseListenerExample2 extends Frame implements MouseListener{  
  4.     MouseListenerExample2(){  
  5.         addMouseListener(this);  
  6.           
  7.         setSize(300,300);  
  8.         setLayout(null);  
  9.         setVisible(true);  
  10.     }  
  11.     public void mouseClicked(MouseEvent e) {  
  12.         Graphics g=getGraphics();  
  13.         g.setColor(Color.BLUE);  
  14.         g.fillOval(e.getX(),e.getY(),30,30);  
  15.     }  
  16.     public void mouseEntered(MouseEvent e) {}  
  17.     public void mouseExited(MouseEvent e) {}  
  18.     public void mousePressed(MouseEvent e) {}  
  19.     public void mouseReleased(MouseEvent e) {}  
  20.       
  21. public static void main(String[] args) {  
  22.     new MouseListenerExample2();  
  23. }  
  24. }  

Output:

java awt mouselistener example 2

ava MouseMotionListener Interface

The Java MouseMotionListener is notified whenever you move or drag mouse. It is notified against MouseEvent. The MouseMotionListener interface is found in java.awt.event package. It has two methods.

Methods of MouseMotionListener interface

The signature of 2 methods found in MouseMotionListener interface are given below:

  1. public abstract void mouseDragged(MouseEvent e);  
  2. public abstract void mouseMoved(MouseEvent e);  

Java MouseMotionListener Example

  1. import java.awt.*;  
  2. import java.awt.event.*;  
  3. public class MouseMotionListenerExample extends Frame implements MouseMotionListener{  
  4.     MouseMotionListenerExample(){  
  5.         addMouseMotionListener(this);  
  6.           
  7.         setSize(300,300);  
  8.         setLayout(null);  
  9.         setVisible(true);  
  10.     }  
  11. public void mouseDragged(MouseEvent e) {  
  12.     Graphics g=getGraphics();  
  13.     g.setColor(Color.BLUE);  
  14.     g.fillOval(e.getX(),e.getY(),20,20);  
  15. }  
  16. public void mouseMoved(MouseEvent e) {}  
  17.   
  18. public static void main(String[] args) {  
  19.     new MouseMotionListenerExample();  
  20. }  
  21. }  

Output:

java awt mousemotionlistener example 1


Java MouseMotionListener Example 2

  1. import java.awt.*;  
  2. import java.awt.event.MouseEvent;  
  3. import java.awt.event.MouseMotionListener;  
  4. public class Paint extends Frame implements MouseMotionListener{  
  5.     Label l;  
  6.     Color c=Color.BLUE;  
  7.     Paint(){  
  8.     l=new Label();  
  9.     l.setBounds(20,40,100,20);  
  10.     add(l);  
  11.       
  12.     addMouseMotionListener(this);  
  13.       
  14.     setSize(400,400);  
  15.     setLayout(null);  
  16.     setVisible(true);  
  17. }  
  18. public void mouseDragged(MouseEvent e) {  
  19.     l.setText("X="+e.getX()+", Y="+e.getY());  
  20.     Graphics g=getGraphics();  
  21.     g.setColor(Color.RED);  
  22.     g.fillOval(e.getX(),e.getY(),20,20);  
  23. }  
  24. public void mouseMoved(MouseEvent e) {  
  25.     l.setText("X="+e.getX()+", Y="+e.getY());  
  26. }  
  27. public static void main(String[] args) {  
  28.     new Paint();  
  29. }  
  30. }  

Output:

java awt mousemotionlistener example 2


Anurag Rana Educator CSE/IT

Comments

Popular posts from this blog

JAVA Scrollbar, MenuItem and Menu, PopupMenu

ava AWT Scrollbar The  object  of Scrollbar class is used to add horizontal and vertical scrollbar. Scrollbar is a  GUI  component allows us to see invisible number of rows and columns. AWT Scrollbar class declaration public   class  Scrollbar  extends  Component  implements  Adjustable, Accessible   Java AWT Scrollbar Example import  java.awt.*;   class  ScrollbarExample{   ScrollbarExample(){               Frame f=  new  Frame( "Scrollbar Example" );               Scrollbar s= new  Scrollbar();               s.setBounds( 100 , 100 ,  50 , 100 );               f.add(s);               f.setSize( 400 , 400 );               f.setLayout( null );               f.setVisible( true );   }   public   static   void  main(String args[]){           new  ScrollbarExample();   }   }   Output: Java AWT Scrollbar Example with AdjustmentListener import  java.awt.*;   import  java.awt.event.*;   class  ScrollbarExample{        ScrollbarExample(){               Frame f=  new  Frame( "Scro

Difference between net platform and dot net framework...

Difference between net platform and dot net framework... .net platform supports programming languages that are .net compatible. It is the platform using which we can build and develop the applications. .net framework is the engine inside the .net platform which actually compiles and produces the executable code. .net framework contains CLR(Common Language Runtime) and FCL(Framework Class Library) using which it produces the platform independent codes. What is the .NET Framework? The Microsoft .NET Framework is a platform for building, deploying, and running Web Services and applications. It provides a highly productive, standards-based, multi-language environment for integrating existing investments with next-generation applications and services as well as the agility to solve the challenges of deployment and operation of Internet-scale applications. The .NET Framework consists of three main parts: the common language runtime, a hierarchical set of unified class librari

C++ this Pointer, static, struct and Enumeration

C++ this Pointer In C++ programming,  this  is a keyword that refers to the current instance of the class. There can be 3 main usage of this keyword in C++. It can be used  to pass current object as a parameter to another method. It can be used  to refer current class instance variable. It can be used  to declare indexers. C++ this Pointer Example Let's see the example of this keyword in C++ that refers to the fields of current class. #include <iostream>    using   namespace  std;   class  Employee {       public :           int  id;  //data member (also instance variable)               string name;  //data member(also instance variable)            float  salary;          Employee( int  id, string name,  float  salary)             {                   this ->id = id;                  this ->name = name;                  this ->salary = salary;            }             void  display()             {                 cout<<id<< "  " <<name<&