Quantcast
Viewing all articles
Browse latest Browse all 3

How to use MouseListener Interface to handle Mouse Events in Java


(adsbygoogle = window.adsbygoogle || []).push({});

Image may be NSFW.
Clik here to view.
How to use MouseListener Interface to handle Mouse Events in Java

How to use MouseListener Interface to handle Mouse Events in Java

Concept: In this Java program the problem is to handle mouse click using the concept of an Applet and learn how to use MouseListener Interface in Java to handle Mouse Events. Also display the coordinate of the clicked point on the status and display the status of mouse event in a label.

Introduction to Mouse Events on Java Applets

Mouse events notify when the user uses the mouse (or similar input device) to interact with a component. Mouse events occur when the cursor enters or exits a component’s onscreen area and when the user presses or releases one of the mouse buttons.

To track mouse events on screen, a java program implements different interface, such as if an application requires the detection of both mouse events and mouse-motion events, use the MouseInputAdapter class. This class implements the MouseInputListener, a convenient interface that implements the MouseListener and MouseMotionListener interfaces. However, the MouseInputListener interface does not implement the MouseWheelListener interface.

Here to explain simple APIs of MouseListener Interface, an example of a class applet is made with name MouseApplet which is derived from the Applet class and uses the interface MouseListener.

Explanation about MouseListener Interface and MouseListener APIs in Java Applet

Methods for MouseListener Interface

Methods Purpose
mouseClicked(MouseEvent) Called just after the user clicks the listened-to component.
mouseReleased(MouseEvent) Called just after the cursor enters the bounds of the listened-to component.
mouseEntered(MouseEvent) Called just after the cursor exits the bounds of the listened-to component.
mousePressed(MouseEvent) Called just after the user presses a mouse button while the cursor is over the listened-to component
mouseExited(MouseEvent) Called just after the user releases a mouse button after a mouse press over the listened-to component.

Methods from MouseEvent Class 

Method Purpose
int getClickCount() Returns the number of quick, consecutive clicks the user has made (including this event). For example, returns 2 for a double click.
int getX()int getY()Point getPoint() Return the (x,y) position at which the event occurred, relative to the component that fired the event.
int getXOnScreen()int getYOnScreen()int getLocationOnScreen() Return the absolute (x,y) position of the event. These coordinates are relative to the virtual coordinate system for the multi-screen environment. Otherwise, these coordinates are relative to the coordinate system associated with the Component’s Graphics Configuration.
int getButton() Returns which mouse button, if any, has a changed state. One of the following constants is returned: NOBUTTON, BUTTON1, BUTTON2, or BUTTON3.
boolean isPopupTrigger() Returns true if the mouse event should cause a popup menu to appear. Because popup triggers are platform dependent, if your program uses popup menus, you should call isPopupTrigger for all mouse-pressed and mouse-released events fired by components over which the popup can appear..
String getMouseModifiersText(int) Returns a String describing the modifier keys and mouse buttons that were active during the event, such as “Shift”, or “Ctrl+Shift”. These strings can be localized using the awt.properties file.

Example: Handling Mouse Event in a Java Applet

//program to handle mouse event in an applet

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class MouseApplet extends Applet implements MouseListener{

	Label l;
	int x,y;
	public void init(){
		addMouseListener(this);
		l = new Label("Testing mouse click");
		add(l);	
	}
	public void mouseClicked(MouseEvent me){
		x = me.getX();
		y = me.getY();
		showStatus("X:"+x+"Y:"+y);	
	}
	public void mouseReleased(MouseEvent me){
		l.setText("Released");
		setBackground(Color.red);	
	}
	public void mousePressed(MouseEvent me){
		l.setText("Pressed");
		setBackground(Color.green);
	}
	public void mouseEntered(MouseEvent me){
		l.setText("Entered");
		setBackground(Color.yellow);
}
	public void mouseExited(MouseEvent me){
		l.setText("Exited");
		setBackground(Color.blue);	
	}
}
/*<applet code="MouseApplet" width="200" height="200"></applet>*/

 Running Mouse Click Event from Applet Viewer

Image may be NSFW.
Clik here to view.
Running Java Applet - Mouse Event Handler from MouseListener Interface

Running Java Applet – Mouse Event Handler from MouseListener Interface

  • First save the java applet as MouseApplet.java
  • Compile the java applet using command
    >javac MouseApplet.java
  • Run MouseApplet.java as applet using command
    >appletviewer MouseApplet.java

This article is posted under series of Java Programming Tutorials to make understandable knowledge of using MouseListener Interface to hander Mouse events on Java.

The post How to use MouseListener Interface to handle Mouse Events in Java appeared first on The Computer Students.


Viewing all articles
Browse latest Browse all 3

Trending Articles