Basic Layout


The FormLayout operates on the outlined area. Components are added to the layout as single components or label/field pairs. If they are added as pairs, a subcolumn is created within the column to align all the fields. Fields and labels can be added with modes to allow them free alignment from the column or subcolumn (FREE_LABEL and FREE_FIELD). Multiple sets of components may be added to a single row and column; in this example, "First Name" and "MI" are both in row 1, column 2, and "City", "State", and "Zip" are all in row 5, column 2. The first of such sets will align as if they were alone in the segment, and the following components will follow without alignment. Extra and limited space issues affect all components in a segment proportionally to each component's stretchability (preferred width - minimum width); see the fillRightPct parameter example for a demonstration.

Panel Construction Code

m_formPanel = new FormPanel(8, 5, 8, 8);
m_formPanel.setDefaultFillRightPct(.3);
// FormPanel extends JPanel,
// has FormLayout as the default layout manager,
// and exposes all the methods of FormLayout


m_formPanel.add(new JLabel("Invoice Item ID"), new JTextField(8), 1, 1);

JComboBox productCB = new JComboBox();
productCB.addItem("Lawn Mower");
m_formPanel.add(new JLabel("Product"), productCB, 2, 1);

m_formPanel.add(new JLabel("Quantity"), new JTextField(3), 3, 1);

JComboBox deliveryCB = new JComboBox();
deliveryCB.addItem("10 Day Ground");
m_formPanel.add(new JLabel("Delivery Method"), deliveryCB, 4, 1);

m_formPanel.add(new JLabel("First Name"), new JTextField(14), 1, 2);
m_formPanel.add(new JLabel("MI"), new JTextField(2), 1, 2);
m_formPanel.add(new JLabel("Last Name"), new JTextField(21), 2, 2);

m_formPanel.add(new JLabel("Street"), new JTextField(30), 3, 2);

m_formPanel.add(new JLabel("City"), new JTextField(15), 4, 2);
m_formPanel.add(new JLabel("State"), new JTextField(2), 4, 2);
m_formPanel.add(new JLabel("Zip"), new JTextField(5), 4, 2, 1);