Neat and tidy Swing with JGoodies FormLayout

Feb 24, 2014
2 minute read

When its comes to laying out slightly complex forms in AWT or Swing, using the baked-in layout managers (BoxLayout, FlowLayout, GridLayout) can feel frustrating.

Thankfully I stumbled across the JGoodies FormLayout layout manager whilst working on some Swing applications using IntelliJ IDEA 13. IDEA has an integrated visual GUI builder, that makes it really quick and easy to mock up FormLayout controlled layouts, but for the most precise level of control I found handcrafting to be very clean and simple.

The JGoodies library is covered under a BSD License, permitting binary redistibution. In essence grid specifications are passed into the FormLayout constructor, by first defining the columns, followed by the rows. The component sizes min, pref, default are used to set column and row sizes that reflect the minimum or preferred sizes of the contained components. If you specify a column size as min sized, FormLayout will ask all components in that column for their minimum width and chooses the largest width as the column width.

Using some simple naming conventions, such as pref or shorthand version p for preferred width/height, or 3dlu meaning 3 dialog units (concept borrowed from Microsoft interface specification many moons ago), or if needed you can decorate terms with justifications (e.g. right, left, middle, etc), and growable column widths with grow.

The default size aims to give a column the width of the largest preferred width. If container space is scarce, it shrinks the column down to the largest minimum width.

On such a high from the FormLayout, I took the opportunitity to explore the JGoodies Looks library, which provides a more cross platform consistent look & feel, and utilities for doing so. One JAR include and a single line of code later, pow:

UIManager.setLookAndFeel(new Plastic3DLookAndFeel());

Here’s some renders on an Windows XP machine (I still have to use XP sometimes…fml), and my Debian GNU/Linux machine.