Home > Articles > Programming > User Interface (UI)

  • Print
  • + Share This
Like this article? We recommend

FormLayout Basics

Like GridBagLayout, FormLayout aligns components vertically and horizontally in a dynamic rectangular grid of cells, with each component occupying one or more cells. The biggest difference between these two layout managers is how rows and columns are specified:

  • GridBagLayout depends on row and column information that has been specified externally via assignments to constraints variables.
  • FormLayout places this information within the layout manager.

The com.jgoodies.forms.layout.FormLayout class provides four constructors for creating FormLayout instances. These constructors let you specify columns and rows via human-readable String descriptions, or via arrays of com.jgoodies.forms.layout.ColumnSpec and com.jgoodies.forms.layout.RowSpec objects. In this article, I focus on the following constructor:

public FormLayout(String encodedColumnSpecs, String encodedRowSpecs)

Each component that FormLayout manages is associated with an instance of the following class:


This object specifies where a component should be located on the layout manager’s grid and how the component should be positioned (centered within its display area, for example). Along with the constraints object, FormLayout considers each component’s minimum and preferred sizes in order to determine a component’s size.

I’ve created an example that demonstrates the most basic way of using FormLayout. This example presents a method that first creates an instance of FormLayout, and then creates an instance of CellConstraints. Moving on, the example creates a javax.swing.JPanel object based on this layout, and populates this container with components and CellConstraints objects that provide positioning information. The panel subsequently returns from this method:

public JPanel buildGUI ()
 FormLayout layout = new FormLayout ("right:pref, 2dlu, pref:grow", // columns
     "pref, 3dlu, pref");  // rows
 CellConstraints cc = new CellConstraints ();

 JPanel panel = new JPanel (layout);
 panel.add (new JLabel ("Name:"), cc.xy (1, 1));
 panel.add (new JTextField (20), cc.xy (3, 1));
 panel.add (new JLabel ("Address:"), cc.xy (1, 3));
 panel.add (new JTextField (20), cc.xy (3, 3));

 return panel;

The FormLayout constructor argument "right:pref, 2dlu, pref:grow" uses a mini language to identify two component columns (1 and 3), separated by a column (2) that provides space between these components. Field right:pref causes the first column’s components to be right-aligned, and for this column to be as wide as the widest component (based on the component’s preferred size). Field 2dlu specifies two dialog units of space for column 2. Finally, pref:grow causes column 3 to grow—encompassing all extra space—as the panel is resized.

The FormLayout constructor argument "pref, 3dlu, pref" identifies two component rows (1 and 3), separated by a spacer row (2). Rows 1 and 3 use pref to identify their heights as the heights of the tallest components in their respective rows. Finally, 3dlu specifies three dialog units of space.

The CellConstraints class provides a variety of methods for specifying component constraints. All of these methods specify a minimum of the component’s row and column, with some methods also specifying the width (column span) and height (row span) of the component’s display area, horizontal alignment, and vertical alignment. For example, the code fragment above calls public CellConstraints xy(int col, int row) to set row and column origins, set row span and column span to 1, and use the default alignments.

  • + Share This
  • 🔖 Save To Your Account