File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes basic question about events Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "basic question about events" Watch "basic question about events" New topic

basic question about events

kashif sohail

Joined: Dec 04, 2000
Posts: 29
AWT events :
For example I have a class
All imports
Public class MouseEvents extends Applets implements MouseListner,MouseMotionListner{
String msg=� �;
Int mouseX=0,mouseY=0;
Public void init() {
addMotionListner (this); // I didn�t understand the use of this Plz explain
addMouseListner (this);
public void mousePressed(MouseEvent me){
consider all other required methods are here (implemented)
public void paint(Graphic g) {
My problem is this I m unable to figure out that
1: what does this do.
2: how mousePressed and other methods r called as I have not given any parameter to them ( actually I think I have just created the method which takes an object of type MouseEvent ) I don�t know how and at what time I have passed the parameter object of type MouseEvent and how I have used the me.getX with out passing parameter ,
did any new instance of object is created some where implicitly as it is the same case with Graphic .

Daniel Searson
Ranch Hand

Joined: Dec 03, 2000
Posts: 83
Okay, the method addMouseListener() wants a reference to a MouseListener object. Because your class has implemented MouseListener (and therefore all of its methods) you are allowed to pass in an instance of your own class. You do this by using the "this" reference.
While your program is running, there is a separate thread also going (created by the JVM) that waits for (or listens for) events like MouseEvents and MouseMotionEvents. When it recieves one, it sends it to all the registered event listeners, including your class. To do this, it calls the method MousePressed() or whatever corresponds to the event generated.
The event listeners know where to find your mousePressed() method because you have given them a reference earlier (through addMouseListener()) to your class. The listener is responsible for creating the MouseEvent object.
Hope that helps.

- Daniel

- Daniel
I agree. Here's the link:
subject: basic question about events
jQuery in Action, 3rd edition