File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "basic question about events" Watch "basic question about events" New topic
Author

basic question about events

kashif sohail
Greenhorn

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){
mouseX=me.getX();
mouseY=me.getY();
msg=�Down�;
repaint();
}
consider all other required methods are here (implemented)
public void paint(Graphic g) {
g.drawstring(msg,mouseX,mouseY);
}
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: http://aspose.com/file-tools
 
subject: basic question about events
 
Similar Threads
MouseMotionListener
TextField Population of Mouse Events?
basic question about events
i'm getting an error start:applet not intialized.
unusual behaviour of subclass of JPanel