简体   繁体   中英

How can I ignore any KeyListeners that may have been added by client programs when certain conditions are met?

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM