• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

removing listeners

 
Paul Keohan
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic