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...)

Here is my sample program I wrote to do this with FlowLayout:

/**
 * FlowLayoutTest.java
 * Copyright (c) 2002 by Dr. Herong Yang
 */
import java.awt.*;
import javax.swing.*;
public class FlowLayoutTest {
   pblic static void main(String[] a) {
      JFrame myFrame = new JFrame("FlowLayout Test");
      myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      Container myPane = myFrame.getContentPane();
      myPane.setLayout(new FlowLayout(FlowLayout.CENTER));
      myPane.add(getFieldPanel());
      myPane.add(getButtonPanel());
      myFrame.pack();
      myFrame.setVisible(true);
   }
   private static JPanel getFieldPanel() {
      JPanel p = new JPanel(new FlowLayout());
      p.setBorder(BorderFactory.createTitledBorder("Details"));
      p.add(new JLabel("Name:"));
      p.add(new JTextField(16));
      p.add(new JLabel("System:"));
      p.add(getSystemPanel());
      p.add(new JLabel("Language:"));
      p.add(getLanguagePanel());
      p.add(new JLabel("Year:"));
      p.add(new JComboBox(new String[] {"2001","2002","2003"}));
      return p;
   }
   private static JPanel getButtonPanel() {
      JPanel p = new JPanel(new FlowLayout());
      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 FlowLayout());
      p.add(unixButton);
      p.add(winButton);
      return p;
   }
   private static JPanel getLanguagePanel() {
      JPanel p = new JPanel(new FlowLayout());
      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? Pretty bad, right?

Initially all components are positioned in a single row. If you narrow the window, "OK" and "Cancel" buttons will be wrapped to the next row. If you narrow it further, no change will happen on the positions. Because the top container, the window, really contains only two components: the field panel and the button panel.

So, FlowLayout is not good for my example.

BoxLayout

java.awt.BoxLayout - A layout that:

  • Takes unlimited number of components.
  • Positions each component next to each other only one direction, horizontal or vertical.
  • Resizes components that are resizable to fill entire container.
  • Resizes components that are resizable when the container is resized.

To test BoxLayout, I wrote another program to try to display my window with BoxLayout:

/**
 * BoxLayoutTest.java
 * Copyright (c) 2002 by Dr. Herong Yang
 */
import java.awt.*;
import javax.swing.*;
public class BoxLayoutTest {
   public static void main(String[] a) {
      JFrame myFrame = new JFrame("BoxLayout Test");
      myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      Container myPane = myFrame.getContentPane();
      myPane.setLayout(new BoxLayout(myPane,BoxLayout.Y_AXIS));
      myPane.add(getFieldPanel());
      myPane.add(getButtonPanel());
      myFrame.pack();
      myFrame.setVisible(true);
   }
   private static JPanel getFieldPanel() {
      JPanel p = new JPanel();
      p.setLayout(new BoxLayout(p,BoxLayout.X_AXIS));
      p.setBorder(BorderFactory.createTitledBorder("Details"));
      p.add(getLabelPanel());
      p.add(getValuePanel());
      return p;
   }
   private static JPanel getButtonPanel() {
      JPanel p = new JPanel();
      p.setLayout(new BoxLayout(p,BoxLayout.X_AXIS));
      p.add(new JButton("OK"));
      p.add(new JButton("Cancel"));
      return p;
   }
   private static JPanel getLabelPanel() {
      JPanel p = new JPanel();
      p.setLayout(new BoxLayout(p,BoxLayout.Y_AXIS));
      JLabel l = new JLabel("Name:");
      l.setAlignmentX(Component.RIGHT_ALIGNMENT);
      p.add(l);
      l = new JLabel("System:");
      l.setAlignmentX(Component.RIGHT_ALIGNMENT);
      p.add(l);
      l = new JLabel("Language:");
      l.setAlignmentX(Component.RIGHT_ALIGNMENT);
      p.add(l);
      l = new JLabel("Year:");
      l.setAlignmentX(Component.RIGHT_ALIGNMENT);
      p.add(l);
      return p;
   }
   private static JPanel getValuePanel() {
      JPanel p = new JPanel();
      p.setLayout(new BoxLayout(p,BoxLayout.Y_AXIS));
      JComponent c = new JTextField(16);
      c.setAlignmentX(Component.LEFT_ALIGNMENT);
      p.add(c);
      JPanel s = getSystemPanel();
      s.setAlignmentX(Component.LEFT_ALIGNMENT);
      p.add(s);
      s = getLanguagePanel();
      s.setAlignmentX(Component.LEFT_ALIGNMENT);
      p.add(s);
      JComboBox b = new JComboBox(
         new String[] {"2001","2002","2003"});
      b.setAlignmentX(Component.LEFT_ALIGNMENT);
      p.add(b);
      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();
      p.setLayout(new BoxLayout(p,BoxLayout.X_AXIS));
      p.add(unixButton);
      p.add(winButton);
      return p;
   }
   private static JPanel getLanguagePanel() {
      JPanel p = new JPanel();
      p.setLayout(new BoxLayout(p,BoxLayout.X_AXIS));
      p.add(new JCheckBox("Java",true));
      p.add(new JCheckBox("C++",true));
      p.add(new JCheckBox("Perl",false));
      return p;
   }
}

(Continued on next part...)

Part:   1  2   3  4 

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