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 ...

Get Java Swing, 2nd Edition 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.