Java Reference
In-Depth Information
Note The focus subsystem had a major overhaul with the 1.4 release of J2SE. All the older guts are still
present, but should be avoided. The older stuff didn't work well and was very buggy. Sun's fix was to essen-
tially throw everything away and start over, but old APIs are still present. In your quest to work with the focus
subsystem, learn to use only the updated APIs, not the older ones. Classes like javax.swing.FocusManager
and javax.swing.DefaultFocusManager are completely obsolete now.
An important concept in focus management is the focus cycle , which maps the focus traversal
order for the closed set of components within a specific Container . The following classes are
also major players in focus management:
FocusTraversalPolicy : A java.awt class that defines the algorithm used to determine the
next and previous focusable components.
KeyboardFocusManager : A java.awt class that acts as the controller for keyboard navigation
and focus changes. To request a focus change, you tell the manager to change focusable
components; you don't request focus on a particular component.
You can find out when the Swing component gets the input focus by registering a
FocusListener . The listener allows you to find out when a component gains or loses focus,
which component lost focus when another component gained it, and which component got
focus when another component lost focus. Additionally, a temporary focus change can happen
for something like a pop-up menu. The component that lost focus will receive it again when
the menu goes down.
The installed focus traversal policy describes how to move between the focusable compo-
nents of a window. By default, the next component is defined by the order in which components
are added to a container, as shown in Figure 2-10. For Swing applications, this focus traversal
starts at the top left of the figure and goes across each row and down to the bottom right. This is the
default policy, LayoutFocusTraversalPolicy . When all the components are in the same container,
this traversal order is called a focus cycle and can be limited to remain within that container.
Note A user can press Tab or Shift-Tab to move forward or backward through the components in a
container, thus transferring the input focus.
 
Search WWH ::




Custom Search