Herong's Tutorial Notes on Swing
Dr. Herong Yang, Version 3.05, 2006

Layouts of Components

Part:   1  2  3   4 

(Continued from previous part...)

Run it. What do you think about the result? Much better, right?

But if you look closely, value components are not aligned to the corresponding label components. It is almost impossible to align them, because they are in two different panels.

So, BoxLayout is still not good for my example.

Another question about BoxLayout is why the constructor needs to take the container as input. Constructors of other layouts do not need containers. This makes the statement looks very strange: p.setLayout(new BoxLayout(p,...)).

GridLayout

java.awt.GridLayout - A layout that:

  • Divides the container into rows and columns. The number of rows and the number of columns are configurable. Rows and columns are equally divided.
  • Places components into the specified cells.
  • Resizes each component to match the size of its cell.
  • Resizes all components when the container is resized.

Again, I wrote another program to try to display my window with GridLayout:

/**
 * GridLayoutTest.java
 * Copyright (c) 2002 by Dr. Herong Yang
 */
import java.awt.*;
import javax.swing.*;
public class GridLayoutTest {
   public static void main(String[] a) {
      JFrame myFrame = new JFrame("Layout Test 1");
      myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      Container myPane = myFrame.getContentPane();
      myPane.setLayout(new GridLayout(2,1));
      myPane.add(getFieldPanel());
      myPane.add(getButtonPanel());
      myFrame.pack();
      myFrame.setVisible(true);
   }
   private static JPanel getFieldPanel() {
      JPanel p = new JPanel(new GridLayout(4,2));
      p.setBorder(BorderFactory.createTitledBorder("Details"));
      p.add(new JLabel("Name:",SwingConstants.RIGHT));
      p.add(new JTextField(16));
      p.add(new JLabel("System:",SwingConstants.RIGHT));
      p.add(getSystemPanel());
      p.add(new JLabel("Language:",SwingConstants.RIGHT));
      p.add(getLanguagePanel());
      p.add(new JLabel("Year:",SwingConstants.RIGHT));
      p.add(new JComboBox(new String[] {"2001","2002","2003"}));
      return p;
   }
   private static JPanel getButtonPanel() {
      JPanel p = new JPanel(new GridLayout(1,2));
      p.add(new JButton("OK"));
      p.add(new JButton("Cancel"));
      return p;
   }
   private static JPanel getSystemPanel() {
      JRadioButton unixButton = new JRadioButton("Unix",true);
      JRadioButton winButton = new JRadioButton("Window",false);
      ButtonGroup systemGroup = new ButtonGroup();
      systemGroup.add(unixButton);
      systemGroup.add(winButton);
      JPanel p = new JPanel(new GridLayout(1,2));
      p.add(unixButton);
      p.add(winButton);
      return p;
   }
   private static JPanel getLanguagePanel() {
      JPanel p = new JPanel(new GridLayout(1,3));
      p.add(new JCheckBox("Java",true));
      p.add(new JCheckBox("C++",true));
      p.add(new JCheckBox("Perl",false));
      return p;
   }
}

Run it. What do you think about the result? You don't like it, right?

All components are aligned correctly in both directions now. But all components are having wrong sizes.

So, GridLayout is still not good for my example.

(Continued on next part...)

Part:   1  2  3   4 

Dr. Herong Yang, updated in 2006
Herong's Tutorial Notes on Swing - Layouts of Components