Advertisement Jump to content
Sign in to follow this  

[java] more than one MouseListener allowed in an applet?

This topic is 4548 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have an applet that implements MouseListener. I have another class that this applet uses that also implements MouseLIstener but it doesnt seem to be working. i tried not implementing the 2nd class w/ mouseListener thinking that my applet-mouselistener would handle ALL mouse-events. but this doesnt seem to be the case either upon testing it. any insights?

Share this post

Link to post
Share on other sites

i have 3 classes.

1) A main class that extends Applet.
2) A Node class (to build a graph with) that extends a custom class, ImageNode.
3) ImageNode extends Component and implements MouseListener.

Within the Applet class I create several Nodes. Each Node has a draw function and draws itself (an image).

What I am trying to do is this:
When i click on the graphical Node and/or hover my mouse over it, I would like to handle the respective mouse event. However I am not getting any mouse events.
And yes, I did addMouseListener(this) within the ImageNode class!

Here's what does work:
If I implement MouseLIstener within the Applet class I get the mouse events. For example, when the applet start and i move my mouse over the applet's canvas, i get a mouseEntered event.

hope this makes my problem/situation clear.

Share this post

Link to post
Share on other sites
Okay, so I came up with a solution, albeit an ugly hack approach.

- I extended JApplet instead of Applet, and thus added each Node that i created to the current content panel.

jApplet.getContentPane().add( new Node() );

- CLass Node added the mouse listener with the 'this' reference within the constructor.

This was my weird result:
If I added 3 Nodes to my JApplet, the first two Nodes would receive the mouse events (a sound effect would play as feedback). However, the 3rd Node would not cause a mouse event to occur. Instead whenever I enter the JApplet itself with the mouse's cursor (it's content panel?) the mouse-event for the 3rd Node would occur.

I verified that it was the 3rd Node's mouse event upon the mouse-cursor entering the JApplet by printing out the mouseEvent.getSource().

So here was my hack solution:
I added an extra Node that is not rendered and does not have a mouse listener.
This weird hack allows all of my Nodes objects to react to mouse events while not allowing my JApplet contentpane itself to react to events.

strange indeed. i cant seem to debug this.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!