سؤال

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?

هل كانت مفيدة؟

المحلول

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 a KeyListener. 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.:

An alternative to key bindings is using key listeners. Key listeners have their place as a low-level interface to keyboard input, but for responding to individual keys key bindings are more appropriate and tend to result in more easily maintained code. Key listeners are also difficult if the key binding is to be active when the component doesn't have focus. Some of the advantages of key bindings are they're somewhat self documenting, take the containment hierarchy into account, encourage reusable chunks of code (Action objects), and allow actions to be easily removed, customized, or shared. Also, they make it easy to change the key to which an action is bound. Another advantage of Actions is that they have an enabled state which provides an easy way to disable the action without having to track which component it is attached to.

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.

نصائح أخرى

  1. KeyBindings (high abstraction)

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


مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top