Swing Mouse Adapter



Swing MouseAdapter

  1. MouseAdapter class is an abstract adapter class in Java Swing.
  2. MouseAdapter class is for receiving mouse events.
  3. Methods specified in MouseAdapter class are empty.
  4. MouseAdapter class exists as convenience for creating listener objects

Class declaration

Below is the declaration for java.awt.event.MouseAdapter class –

public abstract class MouseAdapter
   extends Object
      implements MouseListener, 
                 MouseWheelListener, 
                 MouseMotionListener

Class constructors

MouseAdapter()

Class methods

No.MethodReturn TypeParameterInvoked when
1mouseClicked()voidMouseEventmouse button has been clicked
2mouseDragged()voidMouseEventmouse button is pressed on a component and then dragged
3mouseEntered()voidMouseEventmouse enters a component.
4mouseExited()voidMouseEventmouse exits a component.
5mouseMoved()voidMouseEventmouse cursor has been moved onto a component but no buttons have been pushed
6mousePressed()voidMouseEventmouse button has been pressed on a component
7mouseReleased()voidMouseEventmouse button has been released on a component
8mouseWheelMoved()voidMouseEventmouse wheel is rotated.

Methods inherited

This class inherits methods from the following classes:

java.lang.Object

Mouse Adapter Example

AdapterExample.java

package com.c4learn;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class AdapterExample {
   private JFrame baseFrame;
   private JLabel headLabel;
   private JLabel msgLabel;
   private JPanel controlPanel;

   public AdapterExample() {
      initializeUI();
   }

   public static void main(String[] args) {
      AdapterExample adapterDemo = new AdapterExample();
      adapterDemo.showMouseApapter();
   }

   private void initializeUI() {
      baseFrame = new JFrame("Java Swing Examples");
      baseFrame.setSize(500, 500);
      baseFrame.setLayout(new GridLayout(3, 1));

      headLabel = new JLabel("", JLabel.CENTER);
      msgLabel = new JLabel("", JLabel.CENTER);

      msgLabel.setSize(300, 100);

      baseFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent) {
            System.exit(0);
         }
      });

      controlPanel = new JPanel();
      controlPanel.setLayout(new FlowLayout());

      baseFrame.add(headLabel);
      baseFrame.add(controlPanel);
      baseFrame.add(msgLabel);
      baseFrame.setVisible(true);
   }

   private void showMouseApapter() {
      headLabel.setText("Listener in action : MouseAdapter");

      JPanel panel = new JPanel();
      panel.setBackground(Color.gray);
      panel.setLayout(new FlowLayout());
      
      JLabel msglabel = new JLabel("www.c4learn.com", JLabel.CENTER);

      msglabel.addMouseListener(new MouseAdapter() {
         public void mouseClicked(MouseEvent e) {
            msgLabel.setText("Mouse Clicked: (" + e.getX() + ", "
                  + e.getY() + ")");
         }
      });
      
      panel.add(msglabel);

      controlPanel.add(panel);
      baseFrame.setVisible(true);
   }
}

After running program in the eclipse you will see below output

mouse_adapter