Utility Classes

The utilities presented here are meant to be used with any part of your application. The constants from SwingConstants and static methods of the SwingUtilities class are used throughout the Swing source code and will probably be useful to you as well. You’ll find a lot of these utilities fairly straightforward, and maybe even easy to reproduce with your own code. But try to familiarize yourself with these APIs; they’re meant to keep you from reinventing those wheels with each new application you write.

The SwingUtilities Class

This class serves as a collection point for several methods common in more advanced GUI development projects. You probably won’t use all of the methods in any one application, but some of the methods will doubtless come in handy from time to time. While the purpose of many of these methods is obvious from their signatures, here’s a brief description of the utility calls at your disposal. For a more detailed discussion of the invokeLater( ) and invokeAndWait( ) methods, check out Chapter 28.

Constructor

public SwingUtilities( )

The constructor for SwingUtilities is public, but all of the public methods are static, so you do not need to create an instance.

Class methods

public static Rectangle calculateInnerArea (JComponent c, Rectangle r)

Calculate the position and size of the inner area (excluding the border) of c. The bounds are placed in r and r is returned. This method was introduced in SDK 1.4.

public static Rectangle[] computeDifference(Rectangle ...

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.