[ Team LiB ] Previous Section Next Section

11.2 Containers

The second step in creating a GUI is to place the components you have created and configured into appropriate containers. Chapter 2 of Java Foundation Classes in a Nutshell contains tables that list the container classes available in the AWT and Swing packages. Many of these container classes have specialized uses. JFrame is a top-level window, for example, and JTabbedPane displays the components it contains in individual tabbed panes (like those pictured in Figure 11-1) but Swing and the AWT also define generic container classes, such as JPanel.

Example 11-1 is a listing of Containers.java. This class is a subclass of JPanel. Its constructor method creates a number of other nested JPanel instances, as well as a number of JButton objects contained by those JPanel classes. Example 11-1 illustrates the concept of the containment hierarchy of a GUI, using color to represent the nesting depth of the hierarchy. Figure 11-2 shows what the Containers class looks like when displayed with the ShowBean program as follows:

% java je3.gui.ShowBean je3.gui.Containers
Figure 11-2. Nested containers
Example 11-1. Containers.java
package je3.gui;
import javax.swing.*;
import java.awt.*;

 * A component subclass that demonstrates nested containers and components.
 * It creates the hierarchy shown below, and uses different colors to
 * distinguish the different nesting levels of the containers
 * Containers---panel1----button1
 *        |       |---panel2----button2
 *        |       |        |----panel3----button3
 *        |       |------panel4----button4
 *        |                   |----button5
 *        |---button6
public class Containers extends JPanel {
    public Containers( ) {
        this.setBackground(Color.white);            // This component is white
        this.setFont(new Font("Dialog", Font.BOLD, 24));

        JPanel p1 = new JPanel( );
        p1.setBackground(new Color(200, 200, 200)); // Panel1 is darker
        this.add(p1);                // p1 is contained by this component
        p1.add(new JButton("#1"));   // Button 1 is contained in p1
        JPanel p2 = new JPanel( );
        p2.setBackground(new Color(150, 150, 150)); // p2 is darker than p2
        p1.add(p2);                  // p2 is contained in p1
        p2.add(new JButton("#2"));   // Button 2 is contained in p2
        JPanel p3 = new JPanel( );
        p3.setBackground(new Color(100, 100, 100)); // p3 is darker than p2
        p2.add(p3);                  // p3 is contained in p2
        p3.add(new JButton("#3"));   // Button 3 is contained in p3
        JPanel p4 = new JPanel( );
        p4.setBackground(new Color(150, 150, 150)); // p4 is darker than p1
        p1.add(p4);                  // p4 is contained in p1
        p4.add(new JButton("#4"));   // Button4 is contained in p4
        p4.add(new JButton("#5"));   // Button5 is also contained in p4
        this.add(new JButton("#6")); // Button6 is contained in this component
    [ Team LiB ] Previous Section Next Section