Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

mouseListener instances  RSS feed

 
David Laverty
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I would be grateful for help on creating a class that implements MouseListener, and then adding a single instance of this class to multiple JLabels. I have checked the sun tutorials, as well as my java books, and cant find the correct syntax.

The JLabels i wish to add an instance of a MouseListener to are created in this code. I assume the mouselistener class is added after the objects creation.



using suns tutorial, this is coded in a seperate class



how do i link the two so that 1 instance off mouselistener class is added to the JLabel that is createed?

Regards

david 55

[ October 08, 2006: Message edited by: David Laverty ]
[ October 08, 2006: Message edited by: David Laverty ]
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your code, JFrame implements MouseListener, so each of your JFrames is also its own MouseListener. Instead, it sounds like you want to create a separate single instance of MouseListener to be shared by multiple JFrame instances.

You could do this by creating your own class that implements MouseListener (in which you provide implementation for the MouseListener methods). Create one instance of this class, then use the addMouseListener method in JFrame (inherited from Component) to "connect" it to each of your JFrame instances.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!