5.3.2 Types of Containers


The Container class, like Component is an abstract class. It has two direct subclasses: The most important example of a Panel is an Applet, but you can define as may panels as you wish.

Java distinguishes two kinds of windows:

Frame

The following applet contains a button that pops up a separate window, or more precisely a frame, when pressed.
The Applet
The Java Code

FrameApplet.java
import java.applet.Applet;
import java.awt.*;

public class FrameApplet extends Applet {
  Button openButton;
  InfoWindow window;

  public void init() {
    openButton = new Button("New Window");
    add(openButton);
  }

  public boolean action(Event evt, Object arg) {
    if (evt.target == openButton) {
      window = new InfoWindow("A Simple Window", "Example of a Window");
      window.show();
      return true;
    }
    else {
      return false;
    }
  }
}
The part in the Java code that may need explanation is for displaying the separate window. You create a new InfoWindow object, which will be an extension of a Frame, and invoke the show method in the following way:
window = new InfoWindow("A Simple Window", "Example of a Window");
window.show();

SimpleScratchpad.java
import java.awt.*;

public class InfoWindow extends Frame {
  Button closeButton;
  Label text;

  InfoWindow(String title, String message) {
    super(title);                   // create frame with specified title
    setLayout(new BorderLayout());  // set BorderLayout manager
    text = new Label(message);      // create label with specified text
    closeButton = new Button("Close Window"); // create close button
    add("Center", text);            // add label to frame
    add("South", closeButton);      // add button to frame
    pack();                         // resize frame to preferred size of its components
  }

  public boolean action(Event evt, Object arg) {
    if (evt.target == closeButton) {
      dispose();   // remove frame and give up resources of frame
      return true;
    }
    else {
      return false;
    }
  }

  public boolean handleEvent (Event evt) {
    if (evt.id == Event.WINDOW_DESTROY) {
      dispose();
    }
    return super.handleEvent(evt);
  }
}
The above code defines the InfoWindow class as a subclass of Frame. It contains a Button for closing the window and a label for displaying a short message. When the user clicks on the Close Window button, the window should close and release the resources that are used for the window. This is handled in the action method.

If you want the user to be able to close the window via the tools provided by your platform, e.g. via the Exit button in Windows 95, you need to provide a handler for the Window_DESTROY event. This is done in the handleEvent method.

You can go one step further with closing frames. Suppose that you have a Frame that is a root window for you application and that your application should stop when the window is destroyed, then this you should add the following lines of code to the above handleEvent method right after the invocation of dispose.

SecurityManager security = System.getSecurityManager();
if (security == null) {
  System.exit(0);
}

Panel

The Panel class is mainly used for holding components. The most important example of a Panel is an Applet. Components can be laid out within a container in two ways:

The second way will not be treated because it is unsuitable for applets or for platform-independent programs. Two of the many problems with absolute positioning: the size of components can be different on different platforms and the size of the browser window containing an applet is user-defined and therefore may be not well-suited for the absolute sizes of components. All these problems with laying out components in a container are not present if you use a layout manager. This will be discussed in the next subsection.

Hierarchy of Components

Containers are themselves components. So, they can be placed inside other containers building up a hierarchy of components. Associated with the following applet

is the following hierarchy of components:

So, components in a graphical user interface form a hierarchy. Components are drawn from the top of the hierarchy - in applets, this is the browser window - down to the basic GUI components. This hierarchy of components in an applet or stand-alone application is also used in event handling: events are passed up the component hierarchy until they are handled.