O'Reilly logo

Java Swing by Dave Wood, Marc Loy, Robert Eckstein

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

focusCycleRoot

If true, indicates that the component contains a group of child components all contained within their own focus cycle

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required