четвер, 2 жовтня 2008 р.

Чому не працює Action на KeyEvent.VK_TAB?

Суть проблеми

При спробі навісити на елемент Action що спрацьовує на натиснення Tab або Shift+Tab, Action не спрацьовує.

Причина проблеми


Причина проблеми в тому що AWT по замовчуванню перехоплює всі натиснення Tab або Shift+Tab, щоб перемістити фокус.

Вирішення

Потрібно викликати [Component].setFocusTraversalKeysEnabled(false);
Після цього компонент реагуватиме на Tab або Shift+Tab лише у випадку, коли на ці дії навішений відповідний Action.
Для всіх компонентів

Альтернативний варіант: використати KeyEventDispatcher для перехоплення всіх подій з клавіатури.
Фактично це значить замінити DefaultKeyboardFocusManager на власний.

Альтернативний варіант: змінити клавіші, які переводять фокус:
public void setFocusTraversalKeys(int id, Set<? extends AWTKeyStroke> keystrokes);

Ресурси

Java Tutorials

http://java.sun.com/docs/books/tutorial/uiswing/misc/focus.html
http://java.sun.com/docs/books/tutorial/uiswing/events/keylistener.html
http://java.sun.com/javase/6/docs/api/java/awt/KeyEventDispatcher.html

Java Sun forum

http://forums.sun.com/thread.jspa?messageID=2687174
http://forums.sun.com/thread.jspa?threadID=521328&messageID=2496625

JavaDoc

setFocusTraversalKeysEnabled

public void setFocusTraversalKeysEnabled(boolean focusTraversalKeysEnabled)

Sets whether focus traversal keys are enabled for this Component. Components
for which focus traversal keys are disabled receive key events for focus
traversal keys. Components for which focus traversal keys are enabled do not see
these events; instead, the events are automatically converted to traversal
operations.





Parameters:
focusTraversalKeysEnabled - whether focus traversal keys are
enabled for this Component
Since:
1.4
See Also:
getFocusTraversalKeysEnabled(),
setFocusTraversalKeys(int,
java.util.Set)
, getFocusTraversalKeys(int)


setFocusTraversalKeys


public void setFocusTraversalKeys(int id,
Set<? extends AWTKeyStroke> keystrokes)

Sets the focus traversal keys for a given traversal operation for this
Component.


The default values for a Component's focus traversal keys are
implementation-dependent. Sun recommends that all implementations for a
particular native platform use the same default values. The
recommendations for Windows and Unix are listed below. These
recommendations are used in the Sun AWT implementations.






















Identifier Meaning Default
KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS Normal forward keyboard traversal TAB on KEY_PRESSED, CTRL-TAB on KEY_PRESSED
KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS Normal reverse keyboard traversal SHIFT-TAB on KEY_PRESSED, CTRL-SHIFT-TAB on KEY_PRESSED
KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS Go up one focus traversal cycle none


To disable a traversal key, use an empty Set; Collections.EMPTY_SET is
recommended.


Using the AWTKeyStroke API, client code can specify on which of two
specific KeyEvents, KEY_PRESSED or KEY_RELEASED, the focus traversal
operation will occur. Regardless of which KeyEvent is specified,
however, all KeyEvents related to the focus traversal key, including the
associated KEY_TYPED event, will be consumed, and will not be dispatched
to any Component. It is a runtime error to specify a KEY_TYPED event as
mapping to a focus traversal operation, or to map the same event to
multiple default focus traversal operations.


If a value of null is specified for the Set, this Component inherits the
Set from its parent. If all ancestors of this Component have null
specified for the Set, then the current KeyboardFocusManager's default
Set is used.



Parameters:
id - one of KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,
KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, or
KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS
keystrokes - the Set of AWTKeyStroke for the specified operation
Throws:
IllegalArgumentException - if id is not one of
KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,
KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, or
KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS, or if keystrokes
contains null, or if any Object in keystrokes is not an
AWTKeyStroke, or if any keystroke represents a KEY_TYPED event,
or if any keystroke already maps to another focus traversal
operation for this Component
Since:
1.4
See Also:
getFocusTraversalKeys(int),
KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,
KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS,
KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS






Немає коментарів: