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

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

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