The SpringLayout Class

With SDK 1.4, a new—but not really new—layout manager was added. The SpringLayout manager uses the notion of springs and struts to keep everything in place. A version of SpringLayout existed in the early alpha and betas of the Swing package, but it was not included because the Swing team felt it still needed too much work. While it still needs a bit of work, it has come a long way. Its inclusion in SDK 1.4 is a testament to that progress. The class diagram for SpringLayout and its helpers is shown in Figure 11-17.

The SpringLayout manager classes

Figure 11-17. The SpringLayout manager classes

Before you dive too deeply into this layout manager, you should know that its purpose in life is to aid GUI builders and other code-generating tools. It can certainly be hand-coded—and we have the examples to prove it—but you’ll often leave this layout manager to the aforementioned tools. (If you want a flexible replacement for the GridBagLayout , you might want to take a look at the RelativeLayout manager written by our own Jim Elliott. The complete package with docs, tutorial, and source code can be found on this book’s web site, http://www.oreilly.com/catalog/jswing2/.)

Springs and Struts

Now that you’re here for the long haul, let’s look at the core of the SpringLayout manager’s approach to component layout: spring and struts. A spring is effectively a triplet representing a range of values. It contains ...

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.