Chapter 27. Swing Utilities

There are many tasks you run into that are common and not terribly difficult. Hence, they get rewritten several times in several small variations. Ideally, you would code the task up into a method or class, and keep it around for reuse later. That’s exactly what happened with the SwingUtilities class. It contains several dozen methods to handle some tasks you encounter when dealing with real applications. (There’s a method to test if the mouse button pressed was the left mouse button, for example.)

And how many simple timer classes exist that fire events at useful intervals? Or more likely, how many methods out there contain while() loops wrapped around Thread.sleep(delay) calls to do the work of a clock? The Timer class in the Swing package provides a flexible timer for all your timing needs.

If you use icons, you have probably had to create a “disabled” version of the image for use in your application. Now, the GrayFilter class can help you out. There’s even a tooltip manager class devoted to handling all of the common functions you might expect to have available for tooltips.

We’ve broken these utilities up into several categories, more for presentation than anything. You’ll find the SwingUtilities, SwingConstants, Timer, and ToolTipManager classes in Section 27.1. Section 27.3 covers the KeyStroke and EventListenerList classes. “Editing Utilities” looks at the CellEditor and CellEditorListener interfaces as well as the DefaultCellEditor and KeyStroke ...

Get Java Swing now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.