Layout Managers

Layout Managers

Layout Managers

.

Layout Managers

Layout Managers

.

Custom Layout Manager

.

The LayoutManager Interface Type

public interface LayoutManager 
{
void layoutContainer(Container parent);
Dimension minimumLayoutSize(Container parent);
Dimension preferredLayoutSize(Container parent);
void addLayoutComponent(String name, Component comp);
void removeLayoutComponent(Component comp);
}

Form Layout


FormLayout.java


import java.awt.*;

/**
   A layout manager that lays out components along a central axis
*/
class FormLayout implements LayoutManager
{  
   public Dimension preferredLayoutSize(Container parent)
   {  
      Component[] components = parent.getComponents();
      left = 0;
      right = 0;
      height = 0;
      for (int i = 0; i < components.length; i += 2)
      {
         Component cleft = components[i];
         Component cright = components[i + 1];

         Dimension dleft = cleft.getPreferredSize();
         Dimension dright = cright.getPreferredSize();
         left = Math.max(left, dleft.width);
         right = Math.max(right, dright.width);
         height = height + Math.max(dleft.height,
            dright.height);
      }      
      return new Dimension(left + GAP + right, height);
   }
      
   public Dimension minimumLayoutSize(Container parent)
   {  
      return preferredLayoutSize(parent);
   }

   public void layoutContainer(Container parent)
   {  
      preferredLayoutSize(parent); // sets left, right

      Component[] components = parent.getComponents();

      Insets insets = parent.getInsets();
      int xcenter = insets.left + left;
      int y = insets.top;

      for (int i = 0; i < components.length; i += 2)
      {
         Component cleft = components[i];
         Component cright = components[i + 1];

         Dimension dleft = cleft.getPreferredSize();
         Dimension dright = cright.getPreferredSize();

         int height = Math.max(dleft.height,
            dright.height);

         cleft.setBounds(xcenter - dleft.width, y + (height -
            dleft.height) / 2, dleft.width, dleft.height);

         cright.setBounds(xcenter + GAP, y + (height 
            - dright.height) / 2, dright.width, dright.height);
         y += height;
      }
   }

   public void addLayoutComponent(String name,
      Component comp)
   {}

   public void removeLayoutComponent(Component comp)
   {}

   private int left;
   private int right;
   private int height;
   private static final int GAP = 6;
}


FormLayoutTest.java


import java.awt.*;
import javax.swing.*;

public class FormLayoutTest
{
   public static void main(String[] args)
   {  
      JFrame frame = new JFrame();
      Container contentPane = frame.getContentPane();
      contentPane.setLayout(new FormLayout());
      contentPane.add(new JLabel("Name"));
      contentPane.add(new JTextField(15));
      contentPane.add(new JLabel("Address"));
      contentPane.add(new JTextField(20));
      contentPane.add(new JLabel("City"));
      contentPane.add(new JTextField(10));
      contentPane.add(new JLabel("State"));
      contentPane.add(new JTextField(2));
      contentPane.add(new JLabel("ZIP"));
      contentPane.add(new JTextField(5));
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setVisible(true);
   }
}


Results


Maintained by John Loomis, updated Sun Feb 25 20:38:43 2007