SE450: Form Layout [36/72] Previous pageContentsNext page

file:horstmann/ch05_layout/FormLayout.java [source] [doc-public] [doc-private]
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package horstmann.ch05_layout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.LayoutManager;

/**
   A layout manager that lays out components along a central axis
 */
public 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;
}

file:horstmann/ch05_layout/FormLayoutTester.java [source] [doc-public] [doc-private]
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package horstmann.ch05_layout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

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



Previous pageContentsNext page