The class which processes the MouseEvent should implement this interface.The object of that class must be registered with a component. The object can be registered using the addMouseListener() method.

Interface declaration

Following is the declaration for java.awt.event.MouseListener interface:

  1. public interface MouseListener
  2. extends EventListener

Interface methods

S.N.Method & Description
1 void mouseClicked(MouseEvent e) Invoked when the mouse button has been clicked (pressed and released) on a component.
2 void mouseEntered(MouseEvent e) Invoked when the mouse enters a component.
3 void mouseExited(MouseEvent e) Invoked when the mouse exits a component.
4 void mousePressed(MouseEvent e) Invoked when a mouse button has been pressed on a component.
5 void mouseReleased(MouseEvent e) Invoked when a mouse button has been released on a component.

Methods inherited

This interface inherits methods from the following interfaces:

  • java.awt.EventListener

MouseListener Example

Create the following java program using any editor of your choice in say D:/ > AWT > com > tutorialspoint > gui > AwtListenerDemo.java

  1. package com.tutorialspoint.gui;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5.  
  6. public class AwtListenerDemo {
  7. private Frame mainFrame;
  8. private Label headerLabel;
  9. private Label statusLabel;
  10. private Panel controlPanel;
  11.  
  12. public AwtListenerDemo(){
  13. prepareGUI();
  14. }
  15.  
  16. public static void main(String[] args){
  17. AwtListenerDemo awtListenerDemo = new AwtListenerDemo();
  18. awtListenerDemo.showMouseListenerDemo();
  19. }
  20.  
  21. private void prepareGUI(){
  22. mainFrame = new Frame("Java AWT Examples");
  23. mainFrame.setSize(400,400);
  24. mainFrame.setLayout(new GridLayout(3, 1));
  25. mainFrame.addWindowListener(new WindowAdapter() {
  26. public void windowClosing(WindowEvent windowEvent){
  27. System.exit(0);
  28. }
  29. });
  30.  
  31. headerLabel = new Label();
  32. headerLabel.setAlignment(Label.CENTER);
  33. statusLabel = new Label();
  34. statusLabel.setAlignment(Label.CENTER);
  35. statusLabel.setSize(350,100);
  36.  
  37. controlPanel = new Panel();
  38. controlPanel.setLayout(new FlowLayout());
  39.  
  40. mainFrame.add(headerLabel);
  41. mainFrame.add(controlPanel);
  42. mainFrame.add(statusLabel);
  43. mainFrame.setVisible(true);
  44. }
  45.  
  46. private void showMouseListenerDemo(){
  47. headerLabel.setText("Listener in action: MouseListener");
  48.  
  49. Panel panel = new Panel();
  50. panel.setBackground(Color.magenta);
  51. panel.setLayout(new FlowLayout());
  52. panel.addMouseListener(new CustomMouseListener());
  53.  
  54. Label msglabel = new Label();
  55. msglabel.setAlignment(Label.CENTER);
  56. msglabel.setText("Welcome to TutorialsPoint AWT Tutorial.");
  57.  
  58. msglabel.addMouseListener(new CustomMouseListener());
  59. panel.add(msglabel);
  60.  
  61. controlPanel.add(panel);
  62.  
  63. mainFrame.setVisible(true);
  64. }
  65.  
  66. class CustomMouseListener implements MouseListener{
  67.  
  68. public void mouseClicked(MouseEvent e) {
  69. statusLabel.setText("Mouse Clicked: ("
  70. +e.getX()+", "+e.getY() +")");
  71. }
  72.  
  73. public void mousePressed(MouseEvent e) {
  74. }
  75.  
  76. public void mouseReleased(MouseEvent e) {
  77. }
  78.  
  79. public void mouseEntered(MouseEvent e) {
  80. }
  81.  
  82. public void mouseExited(MouseEvent e) {
  83. }
  84. }
  85. }

Compile the program using command prompt. Go to D:/ > AWT and type the following command.

  1. D:\AWT>javac com\tutorialspoint\gui\AwtListenerDemo.java

If no error comes that means compilation is successful. Run the program using following command.

  1. D:\AWT>java com.tutorialspoint.gui.AwtListenerDemo

Verify the following output

awt_mouselistener.jpg