KappaLayout

Comparison of GridLayout, KappaLayout and LambdaLayout

GridLayout has the property that all components are forced to be the same size, regardless of the preferred size of the component. If the container is stretched, the components are also stretched. KappaLayout attempts to use the preferred size of components, however, it can do a better job than GridLayout. GridLayout is often used for laying out buttons as the property of forcing the buttons to be the same size is desired. KappaLayout has exactly the right properties for laying out a series of buttons:
  1. It examines the columns containing the buttons
  2. It chooses the button with the largest preferred width
  3. It makes all buttons that size (use the "wh" stretch parameter)
  4. It locks the buttons at that size and does not allow resizing
LambdaLayout can be made to operate exactly like GridLayout by setting the stretch parameter to "wh".

Three applet windows should have popped up, one each for GridLayout, KappaLayout, and LambdaLayout. GridLayout and LambdaLayout operate exactly the same, KappaLayout does what is usually desired for a line of buttons.

A fourth applet window, labeled "Fun" shows some of the power of LambdaLayout, because the point is not to do the same thing as GridLayout, but to be able much more and do it easily!

The code for all four applets is listed below. Notice that both KappaLayout and LambdaLayout are only slightly more difficult than GridLayout, but the benefits are well worth the effort.


GridLayout

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.util.*;

public class GridLayoutApplet extends Applet {
   Frame f;
   public void init() {
      // set up a Frame
      f = new Frame("GridLayout Applet");
      f.addWindowListener(new WindowAdapter() {
                             public void windowClosing(WindowEvent we) {
                                f.hide();
                                f.dispose();
                             }
                          });
      Panel p = new Panel();
      p.setLayout(new GridLayout(3, 3));

      p.add(new Button("A"));
      p.add(new Button("AB"));
      p.add(new Button("ABC"));
      p.add(new Button("ABCD"));
      p.add(new Button("ABCDE"));
      p.add(new Button("ABCDEF"));
      p.add(new Button("ABCDEFG"));
      p.add(new Button("ABCDEFGH"));
      p.add(new Button("ABCDEFGHI"));
      f.add(p);
      f.pack();
   }

   public void start() {
      f.show();
   }
}


KappaLayout

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.util.*;

public class Grid_KappaApplet extends Applet {
                     Frame f;
   public void init() {
      // set up a Frame
      f = new Frame("Grid-Kappa Comparison");
      f.addWindowListener(new WindowAdapter() {
                             public void windowClosing(WindowEvent we) {
                                f.hide();
                                f.dispose();
                             }
                          });
      Panel p = new Panel();
      KappaLayout kl = new KappaLayout();
      p.setLayout(kl);

      p.add(new Button("A"),        "0,0,1,1,,wh");
      p.add(new Button("AB"),       "1,0,1,1,,wh");
      p.add(new Button("ABC"),      "2,0,1,1,,wh");
      p.add(new Button("ABCD"),     "0,1,1,1,,wh");
      p.add(new Button("ABCDE"),    "1,1,1,1,,wh");
      p.add(new Button("ABCDEF"),   "2,1,1,1,,wh");
      p.add(new Button("ABCDEFG"),  "0,2,1,1,,wh");
      p.add(new Button("ABCDEFGH"), "1,2,1,1,,wh");
      p.add(new Button("ABCDEFGHI"),"2,2,1,1,,wh");
      int[] same = {0, 1, 2};
      kl.makeRowsSameHeight(same);
      kl.makeColumnsSameWidth(same);
      f.add(p);
      f.pack();
   }

   public void start() {
      f.show();
   }
}


LambdaLayout

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.util.*;

public class Grid_LambdaApplet extends Applet {
                     Frame f;
   public void init() {
      // set up a Frame
      f = new Frame("Grid-Lambda Comparison");
      f.addWindowListener(new WindowAdapter() {
                             public void windowClosing(WindowEvent we) {
                                f.hide();
                                f.dispose();
                             }
                          });
      Panel p = new Panel();
      LambdaLayout kl = new LambdaLayout();
      p.setLayout(kl);
      // make this act exactly like a gridlayout
      p.add(new Button("A"),        "0,0,1,1,,wh");
      p.add(new Button("AB"),       "1,0,1,1,,wh");
      p.add(new Button("ABC"),      "2,0,1,1,,wh");
      p.add(new Button("ABCD"),     "0,1,1,1,,wh");
      p.add(new Button("ABCDE"),    "1,1,1,1,,wh");
      p.add(new Button("ABCDEF"),   "2,1,1,1,,wh");
      p.add(new Button("ABCDEFG"),  "0,2,1,1,,wh");
      p.add(new Button("ABCDEFGH"), "1,2,1,1,,wh");
      p.add(new Button("ABCDEFGHI"),"2,2,1,1,,wh");
      int[] same = {0, 1, 2};
      kl.makeRowsSameHeight(same);
      kl.makeColumnsSameWidth(same);
      f.add(p);
      f.pack();
   }

   public void start() {
      f.show();
   }
}


Fun

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.util.*;

public class LambdaShowoff extends Applet {
   Frame f;
   public void init() {
      // set up a Frame
      f = new Frame("Fun");
      f.addWindowListener(new WindowAdapter() {
                             public void windowClosing(WindowEvent we) {
                                f.hide();
                                f.dispose();
                             }
                          });
      Panel p = new Panel();
      LambdaLayout kl = new LambdaLayout();
      p.setLayout(kl);

      p.add(new Button("AB"), "0,0,1,1,0,wh");
      p.add(new Button("AB"), "0,1,1,1,7,w,");
      p.add(new Button("AB"), "0,2,1,1,0,wh,");
      p.add(new Button("AB"), "1,0,1,1,1,h,");
      p.add(new Button("AB"), "1,1,1,1,0,,");
      p.add(new Button("AB"), "1,2,1,1,5,h,");
      p.add(new Button("AB"), "2,0,1,1,0,wh,");
      p.add(new Button("AB"), "2,1,1,1,3,w,");
      p.add(new Button("AB"), "2,2,1,1,0,wh,");
      f.add(p);
      f.pack();
   }

   public void start() {
      f.show();
   }
}

SourceForge Logo