O'Reilly logo

Java Swing, 2nd Edition by Brian Cole, James Elliott, Marc Loy, Robert Eckstein, Dave Wood

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

Prior to SDK 1.4, focus issues in Java were complicated and not adequately implemented. The problems were deeper than bugs in the code—of which there were certainly plenty, many due to inconsistencies between platforms. The design itself was fundamentally flawed.[1]

Because of these deep changes, the “right way” to do things now differs substantially from previous versions of Java. To avoid confusion, we discuss only the focus model introduced in 1.4. Developers who work with earlier releases should refer to the book’s web site (http://www.oreilly.com/catalog/jswing2) for the first edition’s version of this section.

Here are some highlights of changes in the focus system in 1.4:

  • FocusManager has been deprecated and replaced by KeyboardFocusManager.

  • It’s now possible to learn which component currently has the focus.

  • Lightweight children of Window (not just Frame or Dialog) are able to receive keyboard input.

  • When focus changes, the component gaining focus can find out which one lost it, and vice versa.

  • There is far less platform-dependent code; it has been replaced by a very extensible public API in AWT, with many levels where custom logic can be plugged in. Heavyweight and lightweight focus is much better integrated.

  • Components can lose focus temporarily (e.g., to a scrollbar).

  • It’s a lot easier to work with.

The net result of all these changes is that the system tends to work the way you want it to, and even when you want to add fancy new features, doing so requires less ...

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