views:

21

answers:

1

I haven't had any luck with the web as far as this is concerned, the closest I've come is to read up on the EventQueue, but I can't seem to find a way remove an event by default.

I've overloaded a JTextField so that it displays the remaining characters in a "guessed" word (part of an auto complete component) and when "Enter" is pressed while the JTextField is still "guessing" a word I want it to fill in the remaining portion of the word (this.setText(this.currentGuess);) and ignore any other KeyListeners that may have been added to the component in a client program.

As a test, in the constructor I'm adding two KeyListeners and so far nothing I've done has managed to prevent the second from firing.

+1  A: 

You should be able to use getKeyListeners() to see who's listening and remove all but your desired listener.

Addendum: Also consider alternate implementations, such as the one discussed here.

trashgod
@trashgod I thought I saw somewhere that wasn't really a good way to do it, of course I would want to add them back when I'm done with what I need to do otherwise any the client program would have added would be lost?
izuriel
@izuriel: It's not ideal, and you'd want to document the behavior. I'd look at alternatives; I cited a recent one above.
trashgod
@trashgod Thanks a lot for the information!
izuriel