Implementing MouseListener Interface on NetBeans in Java

This article gives a brief description on MouseListener Interface on NetBeans IDE, and helps in developing an application.

Introduction

 
This article will illustrate an application using the MouseListener Interface in NetBeans IDE. It is a pre-defined interface of the java.awt.event.MouseListener package. The MouseListener interface is used for receiving interesting mouse events like the following:
  • Enter
  • Exit
  • Click
  • Press
  • Release
The MouseListener interface is used to track the preceding events of the mouse on the area occupied by the graphical component.
 

MouseListener methods

 
Since MouseListener is an interface, the body of all the methods of the MouseListener interface must be defined either used or not. There are five MouseListener methods on the various components like Textfield, Button, List and so on. The following methods are used when the event related to the mouse occurs.
  •  mouseEntered(): This method is used when an event occurs on entry of the mouse on any of the components over the Frame.

    Syntax of this method

    public void mouseEntered(MouseEvent e){ }
  • mouseExited(): This method is used when an event occurs on exit of the mouse on any of the specified components over the Frame.

    Syntax of this method

    public void mouseExited(MouseEvent e){ }
  • mouseClicked(): This method is used when an event occurs on clicking of mouse on any of the specified components over the Frame.

    Syntax of this method

    public void mouseClicked(MouseEvent e){ }
  • mousePressed(): This method is used when an event occurs on the long press of the mouse key on any of the specified components over the Frame.

    Syntax of this method

    public void mousePressed(MouseEvent e){ }
  • mouseRlease(): This method is used when an event related to the release of the mouse key occurs after a long press on any of the specified components over the Frame.

    Syntax of this method

    public void mouseReleased(MouseEvent e){ }
This application is based only on MouseListener, Frame and a Textfield. The application is, when the mouse pointer enters the Textfield, the textfield moves from its original place.
 
The source code is as follows
  1. import java.awt.*;  
  2. import java.awt.event.*;  
  3. import java.util.*;  
  4.   
  5. class a extends Frame implements MouseListener {  
  6.   
  7.     TextField t;  
  8.   
  9.     Random and = new Random();  
  10.   
  11.     public a() {  
  12.         setLayout(new FlowLayout());  
  13.         t = new TextField(10);  
  14.         add(t);  
  15.         t.addMouseListener(this);  
  16.         setTitle("Catch The Textfield");  
  17.         setBackground(Color.pink);  
  18.     }  
  19.     public void mouseEntered(MouseEvent e) {  
  20.         t.setBounds(and.nextInt(400), and.nextInt(400), 9020);  
  21.     }  
  22.     public void mouseExited(MouseEvent e) {}  
  23.     public void mouseClicked(MouseEvent e) {}  
  24.     public void mousePressed(MouseEvent e) {}  
  25.     public void mouseReleased(MouseEvent e) {}  
  26. }  
  27. class Mouse {  
  28.   
  29.     public static void main(String arg[]) {  
  30.         a f = new a();  
  31.         f.setSize(400400);  
  32.         f.setVisible(true);  
  33.     }  
  34. }  
In the code above, class Random is initialized to provide random coordinates for the placement of the Textfield at various places.
 
Run the source code
 
fig5.2.jpg
 
Output
 
The following window appears as the output of the source code above.
 
MouseListener in Java
 
When the mouse pointer enters the Textfield, it moves to a random place. The screenshot is shown below.
 
MouseListener in Java