FormLayout.setDefaultFillRightPct

The fillRightPct feature can be used to right justify an entire form. The setDefaultFillRightPct() call on line 2 sets the fillRightPct on every subsequently added component to 1. A fillRightPct of 1 right justifies regardless of preferred width.

Panel Construction Code

m_formPanel = new FormPanel(8, 5, 8, 8);
m_formPanel.setDefaultFillRightPct(1);
// 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);