FormLayout

A Layout manager that can be used to layout 2 columns of components. The alignment and fill type of the columns can be set by using the FormConstraint class.

A couple of predefined FormConstraint objects have been defined:

  • LEFT, Places the component in the left column of the form.
  • RIGHT, Places the component in the right column of the form.
  • RIGHT_FILL, Places the component in the right column of the form and fills the component so it takes up the maximum horizontal space.
  • FULL, The component takes up both the left and right column, and is aligned to the left of the row.
  • FULL_FILL, The component takes up both the left and right column, and is set to fill the whole width of the row.

notes

  • Alignment of components that fill the height or width completely already will not result in a visible change. Ie. the full positioned component will never react to a Vertical alignment setting.
  • fill method will only change the horizontal appearance.
  • Components that are bigger than the available width/height will be LEFT aligned.

Usage

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.Box;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;

import org.bounce.FormLayout;
import org.bounce.FormConstraints;

public class FormLayoutTest {

  public static void main( String[] args) {
    try {
      JPanel panel = new JPanel( new FormLayout( 10, 5));
      panel.setBorder( new EmptyBorder( 10, 10, 10, 10));
      panel.add( new JLabel( "FormLayout using predefined FormConstraints:"), 
                 FormLayout.FULL);
      panel.add( new JLabel( "LEFT"), FormLayout.LEFT);
      panel.add( new JTextField( "RIGHT"), FormLayout.RIGHT);
      panel.add( new JLabel( "LEFT"), FormLayout.LEFT);
      panel.add( new JTextField( "RIGHT_FILL"), FormLayout.RIGHT_FILL);
      panel.add( new JTextField( "FULL"), FormLayout.FULL);
      panel.add( new JTextField( "FULL_FILL"), FormLayout.FULL_FILL);
      
      panel.add( Box.createVerticalStrut( 20), FormLayout.FULL);
      
      panel.add( new JLabel( "FormLayout using custom FormConstraints:"), 
                 FormLayout.FULL);
      
      JTextPane text = new JTextPane();
      text.setText( "LEFT");
      text.setPreferredSize( new Dimension( 100, 50));
      text.setBorder( new EtchedBorder());
      panel.add( text, FormLayout.LEFT);
      
      panel.add( new JTextField( "RIGHT, CENTER, BOTTOM"), 
                 new FormConstraints( FormConstraints.RIGHT, 
                                      FormConstraints.CENTER, 
                                      FormConstraints.BOTTOM));
      
      panel.add( new JLabel( "LEFT, RIGHT"), 
                  new FormConstraints( FormConstraints.LEFT, FormConstraints.RIGHT));
      panel.add( new JTextField( "RIGHT, RIGHT"), 
                  new FormConstraints( FormConstraints.RIGHT, FormConstraints.RIGHT));
      
      JFrame f = new JFrame( "FormLayoutTest");
      f.getContentPane().setLayout( new BorderLayout());
      f.getContentPane().add( panel, BorderLayout.CENTER);
      
      f.setSize( new Dimension( 400, 300));
      f.setVisible( true);
    } catch ( Throwable e) {
      e.printStackTrace();
      System.exit( 1);
    }
  }
}