This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

basic question about events  RSS feed

kashif sohail
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!