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
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "removing listeners" Watch "removing listeners" New topic

removing listeners

Paul Keohan
Ranch Hand

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

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.

Cindy Glass
"The Hood"

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:
subject: removing listeners
It's not a secret anymore!