Working with Focus
And now
for something completely different. In Chapter 3 we offered a terse
discussion of various properties of JComponent
that can be used to control how a component handles focus. Here, we
look at each of those properties more in-depth, and discuss some of
the peculiarities of using focus in Swing.
You’re probably aware that you can shift the focus around in Swing using the TAB key and the SHIFT-TAB key. The path that the focus follows from one component to the next is called the focus cycle. The focus cycle is determined by the focus manager. By default, the focus cycle is dictated by the position of the components in the container: those components with a smaller x,y position always get the focus first. Consequently, with the help of the Swing focus manager, the default focus will cycle from left to right and top to bottom — much like reading a book. The TAB key moves the focus one step ahead in its current focus cycle. SHIFT-TAB moves it one step back.
Five properties in
JComponent
deal with focus. It is important to
note that they are used only in conjunction with the Swing focus
manager class, DefaultFocusManager
. The properties
are summarized in Table 28.5. Let’s go over
each of these properties and discuss how they impact the focus in
Swing.
Table 28-5. Focus-Related Properties in JComponent
Property |
Description |
---|---|
|
If true, indicates that the component contains a group of child components all contained within their own focus cycle |
Get Java Swing now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.