Java Reference
In-Depth Information
KeyboardFocusManager Class
The abstract KeyboardFocusManager class in the AWT library serves as the control mechanism
framework for the input focus behavior of Swing components. The DefaultKeyboardFocusManager
is the concrete implementation. The focus manager allows you to both programmatically
discover who currently has the input focus and to change it.
The component with the current input focus is called the focus owner . This is accessible
via the focusOwner property of KeyboardFocusManager . You can also discover the focusedWindow
and activeWindow properties. The focused window is the window containing the focus owner, and
the active window is either the focused window or the frame or dialog containing the focus owner.
The simple concept of moving to the previous or next component is supported in many
different ways. First, you can use the shortcut API methods of Component and Container :
Component.transferFocus()
Component.transferFocusBackward()
Component.transferFocusUpCycle()
• Container.transferFocusDownCycle()
The first two methods request focus to move to the next or previous component, respec-
tively. The up and down cycle methods request that you move up out of the current focus cycle
or down into the next cycle.
The following methods map directly to methods of the KeyboardFocusManager :
• focusNextComponent()
• focusPreviousComponent()
• upFocusCycle()
• downFocusCycle()
A second set of the same four methods accepts a second parameter of a Component . If the
component isn't specified, these methods change the focused component based on the current
focus owner. If a component is provided, the change is based on that component.
Tab and Shift-Tab are used for keyboard focus traversal because they are defined as the
default focus traversal keys for most, if not all, components. To define your own traversal keys,
you can replace or append to a key set via the setFocusTraversalKeys() method of Component .
Different sets are available for forward, backward, and up-cycle, as specified by the FORWARD_
TRAVERSAL_KEYS , BACKWARD_TRAVERSAL_KEYS , and UP_CYCLE_TRAVERSAL_KEYS constants of
KeyboardFocusManager . You can set and get key sets for each. For instance, to add the F3 key
as an up-cycle key for a component, use the following code:
Set<AWTKeyStroke> set = component.getFocusTraversalKeys(
KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS);
KeyStroke stroke = KeyStroket.getKeyStroke("F3");
set.add(stroke);
component.setFocusTraversalKeys(KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS, set);
Search WWH ::




Custom Search