KappaLayout

Comparison of KappaLayout and LambdaLayout

Two windows should have appeared, each containing a Java applet. Both show the same dialog, and at first appearance are identical. Resizing the frames will show the difference. Note that both layouts respect the preferred size of the components for their minimum size.

Source code for these two applets is shown below. Note that there is almost no difference between the two, which means that it is a simple matter of changing "KappaLayout" to "LambdaLayout" to change the functionality. Notice too that the LambdaLayout example also uses a KappaLayout for the two buttons at the bottom. 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
  4. It locks the buttons at that size and does not allow resizing
This makes the buttons uniform in size and appearance, button text is never truncated nor are the buttons allowed to be too large.

Here's the source code:
KappaLayout ExampleLambdaLayout Example
// $Id: layout_comparison.html,v 1.1.1.1 2002/07/07 17:21:02 daleanson Exp $

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

/**
 * Another example of KappaLayout showing a dialog that has always been
 * difficult to do nicely -- getting the buttons to center on the lists vertically
 * is a hassle and getting the lists to be the same width and height is similarly
 * difficult. Perhaps most difficult is centering the bottom buttons. KappaLayout
 * makes this easy.
 */
public class KappaLayoutTest3 extends Applet {
   Frame f;
   public void init() {
      // set up a Frame
      f = new Frame("KappaLayout Test 3");
      f.addWindowListener(new WindowAdapter() {
                             public void windowClosing(WindowEvent we) {
                                f.dispose();
                             }
                          });
      KappaLayout ll = new KappaLayout();
      Panel p = new Panel();
      p.setLayout(ll);

      /*
      Design:
       012
      0L L     L = Labels
      1C C     C = List, span 6 rows
      2C C
      3CBC     B = Button, ->
      4CBC     B = Button, <-
      5C C
      6C C
      7PPP     P = Panel w/Buttons, span 3 columns
      */
      p.add("0,0,,1,7,w,2", new Label("Pick something from this list:"));
      p.add("2,0,,1,7,w,2", new Label("Selected items:"));
      p.add("0,1,,6,,wh,5", new List(10));
      p.add("2,1,,6,,wh,5", new List(10));
      p.add("1,3", new Button("->"));
      p.add("1,4", new Button("<-"));
      ll.makeColumnsSameWidth(0,2);

      KappaLayout kl = new KappaLayout();
      Panel button_panel = new Panel();
      button_panel.setLayout(kl);
      button_panel.add("0,1,,,,w", new Button("OK"));
      button_panel.add("1,1,,,,w", new Button("Cancel"));
      kl.makeColumnsSameWidth(0,1);
      p.add("0,7,3,1", button_panel);

      f.add(p);
      f.pack();
   }

   public void start() {
      f.show();
   }
}
// $Id: layout_comparison.html,v 1.1.1.1 2002/07/07 17:21:02 daleanson Exp $

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

/**
 * Another example of KappaLayout showing a dialog that has always been
 * difficult to do nicely -- getting the buttons to center on the lists vertically
 * is a hassle and getting the lists to be the same width and height is similarly
 * difficult. Perhaps most difficult is centering the bottom buttons. KappaLayout
 * makes this easy.
 */
public class LambdaLayoutApplet3 extends Applet {
   Frame f;

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

      /*
      Design:
       012
      0L L     L = Labels
      1C C     C = List, span 6 rows
      2C C
      3CBC     B = Button, ->
      4CBC     B = Button, <-
      5C C
      6C C
      7PPP     P = Panel w/Buttons, span 3 columns
      */
      Label label = new Label("Pick something from this list:");
      p.add("0,0,,1,7,w,2", label);
      p.add("2,0,,1,7,w,2", new Label("Selected items:"));
      p.add("0,1,,6,,wh,5", new List(10));   // stretch in both directions
      p.add("2,1,,6,,wh,5", new List(10));   //  ''
      Button right_btn = new Button("->");
      p.add("1,3,,,5", right_btn);           // move to bottom of cell
      p.add("1,4,,,1", new Button("<-"));    // move to top of cell
      ll.makeColumnsSameWidth(0,2);          // make list columns same width


      KappaLayout kl = new KappaLayout();
      Panel button_panel = new Panel();
      button_panel.setLayout(kl);
      button_panel.add("0,1,,,,w", new Button("OK"));
      button_panel.add("1,1,,,,w", new Button("Cancel"));
      kl.makeColumnsSameWidth(0,1);
      p.add("0,7,3,1", button_panel);
      f.add(p);
      // call pack() once to be able to get preferred sizes of some components,
      // this is the best way to use setRowHeight and setColumnWidth, that is,
      // use the preferred width or height of a component. Call pack() afterwards
      // to cause the layout manager to layout the components again.
      f.pack();
      ll.setRowHeight(0, label.getPreferredSize().height);
      ll.setRowHeight(7, button_panel.getPreferredSize().height);
      ll.setColumnWidth(1, right_btn.getPreferredSize().width);
      f.pack();
   }

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

SourceForge Logo