This week's book giveaway is in the Agile forum.
We're giving away four copies of Head First Agile and have Andrew Stellman & Jennifer Greene on-line!
See this thread for details.
Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

methods in interfaces  RSS feed

 
kanna chakravarthi
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear sir/Madam,

Refer attached document
please tell me how the following methods are being invoked
"public void mouseClicked(MouseEvent me)"
"public void mouseEntered(MouseEvent me)".... etc
when i am clicking the mouse button, how the "public void mouseClicked(MouseEvent me)" is being called

whats the connection between my mouse button and that method.
as interfaces only define the methods and we have to implement and add the logic inside that method,then how this method is being invoked when my mouse is clicked, i didnot write any code to let the jvm invoke this particular method when i press the button
as in applets there are no instances created JVM will invoke the methods but how does JVM knows to invoke that particular method when i clicked the mouse button.

waiting for reply and thank you very much in advance

Rgds
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Refer attached document

http://docs.oracle.com/javase/tutorial/uiswing/events/intro.html
 
Campbell Ritchie
Marshal
Posts: 55796
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
kanna chakravarthi
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Dunn wrote:Refer attached document

http://docs.oracle.com/javase/tutorial/uiswing/events/intro.html



sir i came to know how to handle events but coudnt understand whats actually happening inside when i am clicking a mouse button
mouseListener interface is having 5 methods but when i click the mouse button the mouseClicked method is being invoked
why not another method such as mouseEntered is not being invoked.how this exact mapping of my mouse and these methods are
done.how this is being done
 
kanna chakravarthi
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you sir very glad to hear from you
 
kanna chakravarthi
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:And welcome to the Ranch


thank you sir
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> why not another method such as mouseEntered is not being invoked

how do you know that to be true?

post some code showing that mouseEntered does not fire
 
kanna chakravarthi
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Dunn wrote:> why not another method such as mouseEntered is not being invoked

how do you know that to be true?

post some code showing that mouseEntered does not fire


please find the attached filw sir
 
kanna chakravarthi
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="mouseevents" width=300 height=300>
</applet>
*/

public class mouseevents extends Applet implements MouseListener,MouseMotionListener
{
String msg="";
int mouseX=0,mouseY=0;

public void init()
{
addMouseListener(this);
addMouseMotionListener(this);
}
public void mouseClicked(MouseEvent me)
{
mouseX=0;
mouseY=10;
msg="mouseclicked";
repaint();
}

public void mouseEntered(MouseEvent me)
{
mouseX=0;
mouseY=10;
msg="mouseentered";
repaint();
}

public void mouseExited(MouseEvent me)
{
mouseX=0;
mouseY=10;
msg="mouseExited";
repaint();
}

public void mousePressed(MouseEvent me)
{
mouseX=me.getX();
mouseY=me.getY();
msg="down";
repaint();
}

public void mouseReleased(MouseEvent me)
{
mouseX=me.getX();
mouseY=me.getY();
msg="up";
repaint();
}

public void mouseDragged(MouseEvent me)
{
mouseX=me.getX();
mouseY=me.getY();
msg="*";
showStatus("dragging mouse at"+mouseX+","+mouseY);
repaint();
}

public void mouseMoved(MouseEvent me)
{
showStatus("mouse moving at"+me.getX()+","+me.getY());
}

public void paint(Graphics g)
{
g.drawString(msg,mouseX,mouseY);
}
}
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the posted code proves all methods fire, so what's the problem?
 
kanna chakravarthi
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sir yes all methods are firing when i do the appropriate operation on the output screen.
my question is how the mouseClicked method is being fired when i click the mouse button.How this invocation is being done.
actually we should call a method without any call how this method is being invoked.will jvm observe the mouse operations and invoke ?
if so when i click my mouse button how does jvm know to invoke mouseClicked method particularly.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Swing is 'event' driven - all events are fired.

your program registers listeners for specific events it wants to be notified when happened (mouse/key/action/whatever).

here's a simple program to play around with.
click anywhere on the panel (there's no mouseListener 'added' to the panel).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!