FormLayout Versus GridBagLayout
Now that you’ve been introduced to both GridBagLayout and FormLayout, it’s natural to compare these layout managers. I’ve already identified the single GridBagLayout advantage over FormLayout (in terms of FormLayout’s PanelBuilder class). However, there are also advantages to using this layout manager and associated classes—advantages that also point out various design deficiencies with GridBagLayout, such as the following:
- Label mnemonic specification. FormLayout’s PanelBuilder and DefaultFormBuilder classes make it easy to specify label mnemonics (via &). In contrast, GridBagLayout requires label mnemonics to be specified in explicit method calls.
- Label association with data-entry components. FormLayout’s DefaultFormBuilder class automatically associates a label with its data-entry component. In contrast, GridBagLayout requires this association to be made through an explicit method call.
- Spacing units. FormLayout supports dialog units so that the overall proportions of the layout don’t change when viewed on screens with different resolutions, or with different fonts (or font sizes). In contrast, GridBagLayout supports pixel units. (FormLayout optionally supports pixel units.)
- Grid specification. The FormLayout class allows the layout grid to be specified internally through a mini language. In contrast, GridBagLayout requires the grid to be specified through external constraints objects.
- Error handling. FormLayout provides robust error handling. (You’re notified if a component is placed in the wrong cell, for example.) In contrast, GridBagLayout provides minimal error handling—assigning an illegal value to a constraints variable causes an exception.