• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

methods in interfaces

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 4632
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Refer attached document

http://docs.oracle.com/javase/tutorial/uiswing/events/intro.html
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
kanna chakravarthi
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic