Java Reference
In-Depth Information
• getComponentAfter(Container aContainer, Component aComponent)
• getComponentBefore(Container aContainer, Component aComponent)
• getDefaultComponent(Container aContainer)
• getFirstComponent(Container aContainer)
• getInitialComponent(Window window)
getLastComponent(Container aContainer)
Swing provides five predefined traversal policies, as listed in Table 2-5. By picking the right
traversal policy for your application, or rolling your own, you can determine how users will
navigate around the screens.
Table 2-5. Predefined Traversal Policies
Policy
Description
ContainerOrderFocusTraversalPolicy
The components are traversed in the order they are
added to their container. The component must be
visible, displayable, enabled, and focusable to be
part of the focus cycle.
DefaultFocusTraversalPolicy
The default policy for AWT programs, this extends
ContainerOrderFocusTraversalPolicy to check
with the component peer (the operating system) if
the component hasn't explicitly set focusability.
The focusability of a peer depends on the Java
runtime implementation.
InternalFrameFocusTraversalPolicy
Special policy for JInternalFrame , with behavior to
determine initial focusable component based on
the default component of the frame.
SortingFocusTraversalPolicy
Here, you provide a Comparator to the policy
constructor to define the focus cycle order.
LayoutFocusTraversalPolicy
The default policy for Swing programs, this takes
into account geometric settings of components
(height, width, position), and then goes top down,
left to right to determine navigation order. The top-
down, left-right order is determined by the current
ComponentOrientation setting for your locale. For
instance, Hebrew would be in right-left order instead.
To demonstrate, the program in Listing 2-13 reverses the functionality of Tab and Shift-
Tab. When you run the program, it looks the same as the screen shown earlier in Figure 2-11,
with the 3×3 set of buttons. However, with this version, the initial focus starts on the 9 button,
and pressing Tab takes you to 8, then 7, and so on. Shift-Tab goes in the other, more normal,
order.
Search WWH ::




Custom Search