This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Swing / AWT / SWT and the fly likes removing listeners Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "removing listeners" Watch "removing listeners" New topic
Author

removing listeners

Paul Keohan
Ranch Hand

Joined: Mar 15, 2000
Posts: 411
I have the following code in a method:
getComponent(0).removeKeyListener(popupListener);
ListenerPerk popupListener = new ListenerPerk(pLabels, this);
getComponent(0).addKeyListener(popupListener);

getComponent(0) gives me a valid component.
This method does not seem to be removing my listeners. Everytime I call this method I should end up with one KeyListener??? Instead, I end up with several depending on how many times the method is called. Why isn't removeKeyListener() working?
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Paul,
Not sure what you are doing wrong, but RemoveKeyListener works. Check out the example below. How to run it:
1. Press change button first
2. Click inside anywhere not on button
3. Hit any key and see print
4. Press change button
5. Click inside anywhere not on button
6. Hit any key and see print
If you comment out the RemoveKeyListener you will see multiple prints for each key stroke.

Regards,
Manfred.
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
I would guess that since you are using relative component referencing that you are "removing" the listener from a different component than you think that you are.


"JavaRanch, where the deer and the Certified play" - David O'Meara
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: removing listeners