Focus Navigation
We’ve brought up the topic of focus many times in our discussion so far, and we’ve told you that the handling and user navigation of focus is mostly done automatically. The focus system is very powerful and can be heavily customized through the use of “focus traversal policy” objects that control keyboard navigation. For typical application behavior, you won’t have to deal with this directly, but we’ll explain a few features you should know about.
Swing handles keyboard focus navigation through the KeyboardFocusManager
class. This class uses FocusTraversalPolicy
“strategy” objects that implement the actual schemes for locating the next
component to receive focus. There are two primary FocusTraversalPolicy types supplied with Java.
The first, DefaultFocusTraversalPolicy, is part of the AWT
package. It emulates the legacy AWT-style focus management that navigated
components in the order in which they were added to their container. The
next, LayoutFocusTraversalPolicy, is the default for
all Swing applications. It examines the layout and attempts to provide the
more typical navigation from left to right and top to bottom, based on
component position and size.
The focus traversal policy is inherited from containers and oriented
around groups of components known as “root cycles.” By default, each
individual window and JInternalFrame is its own root cycle. In other words, focus traverses all of its child components repeatedly (jumping from the last component back to the ...