Java MouseListener in AWT
Last Updated :
13 Nov, 2023
The Abstract Window Toolkit (AWT) in Java provides a collection of user interface components and event-handling features for creating graphical user interfaces (GUIs). User interaction is an important component of GUI programming, and the MouseListener interface in AWT is an important tool for managing mouse events in Java applications. In this article, we will understand about the MouseListener interface in detail.
Java AWT MouseListener
The MouseListener interface is part of the ‘java.awt.event’ package. It is used to retrieve and respond to mouse-related events in Java applications. Mouse clicks, mouse button presses and releases, mouse enter and exit events are examples of these events. By implementing the MouseListener interface, you can define specific actions that your application has to perform in response to the interactions with the mouse.
Syntax of MouseListener
The following syntax is used to declare a Java MouseListener interface:
public interface MouseListener extends EventListener
Methods of MouseListener Interface
Below is the list of all the methods associated with AWT MouseListener with its description
void mouseClicked(MouseEvent e)
|
Triggered when the mouse button is clicked (pressed and released) on a component.
|
void mousePressed(MouseEvent e)
|
Triggered when a mouse button is pressed on a component.
|
void mouseReleased(MouseEvent e)
|
This method is called when a mouse button is released on a component.
|
void mouseEntered(MouseEvent e)
|
Invoked after mouse entry into a component.
|
void mouseExited(MouseEvent e)
|
Invoked when the mouse exits a component.
|
Examples of Java AWT MouseListener
Here are some examples that will help you to understand the demo of the MouseListener interface easily.
Example 1:
This is a simple example to implement a MouseListener with overriding all the methods. The label is updated at every stage showing the appropriate status of the Mouse event.
Java
import java.awt.*;
import java.awt.event.*;
public class Main {
public static void main(String[] args){
Frame f = new Frame( "MouseListener Demo" );
Label l = new Label( "Welcome to GeeksforGeeks!" );
l.setBounds( 80 , 100 , 220 , 30 );
l.setFont( new Font( "Serif" , Font.BOLD, 18 ));
l.setForeground(Color.GREEN);
l.addMouseListener( new MouseListener() {
public void mouseClicked(MouseEvent e){
l.setText( "Mouse Clicked" );
}
public void mouseEntered(MouseEvent e){
l.setText( "Mouse Entered" );
}
public void mouseExited(MouseEvent e){
l.setText( "Mouse Exited" );
}
public void mousePressed(MouseEvent e){
l.setText( "Mouse Pressed" );
}
public void mouseReleased(MouseEvent e){
l.setText( "Mouse Released" );
}
});
f.add(l);
f.setLayout( null );
f.setSize( 400 , 300 );
f.setVisible( true );
}
}
|
Commands to run MouseListener code:
javac Main.java
java Main
AWT MouseListener Output:
Example 2:
This example is of a simple drawing application using Java AWT MouseListener. Users can draw green squares by clicking the mouse in the given Frame.
Java
import java.awt.*;
import java.awt.event.*;
public class Main implements MouseListener {
Frame frame;
Label l;
Main(){
frame = new Frame( "MouseListener Example" );
l = new Label( "Welcome to GeeksforGeeks!" );
l.setBounds( 70 , 50 , 250 , 20 );
l.setAlignment(Label.CENTER);
l.setFont( new Font( "Serif" , Font.BOLD, 18 ));
l.setForeground(Color.GREEN);
frame.add(l);
frame.addMouseListener( this );
frame.setSize( 400 , 400 );
frame.setLayout( null );
frame.setVisible( true );
}
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e){
Graphics g = frame.getGraphics();
g.setColor(Color.GREEN);
g.fillRect(e.getX(), e.getY(), 30 , 30 );
}
public void mouseReleased(MouseEvent e) {}
public static void main(String[] args) { new Main(); }
}
|
AWT MouseListener Output:
Share your thoughts in the comments
Please Login to comment...