I note that in Java / Swing there seem to be at least two different ways of handling key events:
What are the advantages / disadvantages of each, and when should you prefer one rather than the other?
I note that in Java / Swing there seem to be at least two different ways of handling key events:
What are the advantages / disadvantages of each, and when should you prefer one rather than the other?
Prefer Key Bindings since they were introduced. A
KeyListener
is a lower level connection with events.That page for the key bindings covers a lot of the reasons I would tend to use them rather than a
KeyListener
. It lists many things which are simply 'not available' to aKeyListener
. E.G. choices of:WHEN_FOCUSED
WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
WHEN_IN_FOCUSED_WINDOW
The more I read the linked document, the less I can understand the need to ask the question. E.G.:
Text components
As noted by @Robin, text components also have
DocumentListener
&DocumentFilter
which can be added for functionality more suited to text documents. See Text Component Features for more information on document listeners & filters.advantages
settable, shareable,
designated for simple shortcuts, without un_wanted side effects (most of those events are quite simple and settable)
confortly solving any issue with focus in window (settable too, sure in Java the window must has focus on the screen)
Swing internally to use KeyBindings, built_in shortcuts, actions, more in Key Bindings by @camickr (in Swing are implemented interesting shortcuts & actions)
output should be to the Swing Action (the same high possible abstraction in Swing)
disadvantages
not possible to override all keys from keyboards
not possible to override three or more keys are pressed in the same time
code looks like as very complicated (not true at all, code is sorter in most cases in compare with the same code from KeyListener)
removed for more details to see commnent by @camickr (required Swing Timer for repeated actions)
not possible to consume() with one method implemented in API
.
KeyListeners (low level listener)
advantages
very easy to use, intuitive
code is very short for one two key events
not required any knowledge about Swing, Java
is possible to override tree or more keys are pressed (e.g.), for very complicated keyshortcuts, then there doesn't matter which one can firing any, separate key events
is possible to programatically to event.consume()
is possible to listening for non_finalized, internal events from Compound JComponents (JComboBox, JSpinner ...)
disadvantages
not accesible for part of containers and JComponents
(J)Component should be focus owner and must be focusable
not designated for Swing JComponents
.
AWTEventListener
to combine all Key and Mouse events, low level listener as is possible in Java
basically there isn't reason to use this listener for most of (even are very complicated) GUI based on Swing
I see this listener implemented in custom Componets based on AWT required peers came from native OS
but there are excelent implementations for AWTEventListener Application Inactivity and Global Event Listeners by @camickr
.
notice: ordering of Key events is different platform by platform