Containers

Contents:
Container
Panel
Insets
Window
Frames
Dialogs
FileDialog

This chapter covers a special type of Component called Container. A Container is a subclass of Component that can contain other components, including other containers. Container allows you to create groupings of objects on the screen. This chapter covers the methods in the Container class and its subclasses: Panel, Window, Frame, Dialog, and FileDialog. It also covers the Insets class, which provides an internal border area for the Container classes.

Every container has a layout associated with it that controls how the container organizes the components in it. The layouts are described in Layouts.

Java 1.1 introduces a special Container called ScrollPane. Because of the similarities between scrolling and ScrollPane, the new ScrollPane container is covered with the Scrollbar class in Scrolling.

Container

Container is an abstract class that serves as a general purpose holder of other Component objects. The Container class holds the methods for grouping the components together, laying out the components inside it, and dealing with events occurring within it. Because Container is an abstract class, you never see a pure Container object; you only see subclasses that add specific behaviors to a generic container.

Container Methods

Constructors

The abstract Container class contains a single constructor to be called by its children. Prior to Java 1.1, the constructor was package private.

Grouping

A Container holds a set of objects within itself. This set of methods describes how to examine and add components to the set.

Layout and sizing

Every container has a LayoutManager. The LayoutManager is responsible for positioning the components inside the container. The Container methods listed here are used in sizing the objects within the container and specifying a layout.

Event delivery

The event model for Java is described in Events. These methods help in the handling of the various system events at the container level.

Listeners and 1.1 event handling

With the 1.1 event model, you register listeners, which are told when events occur. Container events occur when a component is added or removed.

// Java 1.1 only import java.awt.*; import java.applet.*; import java.awt.event.*;
public class NewButtonTest11 extends Applet implements ActionListener {
 Button b;
public void init () {
 enableEvents (AWTEvent.CONTAINER_EVENT_MASK); add (b = new Button ("One")); add (b = new Button ("Two")); add (b = new Button ("Three")); add (b = new Button ("Four"));
}
protected void processContainerEvent (ContainerEvent e) {
 if (e.getID() == ContainerEvent.COMPONENT_ADDED) {
 if (e.getChild() instanceof Button) {
 Button b = (Button)e.getChild(); b.addActionListener (this);
}
}
}
public void actionPerformed (ActionEvent e) {
 System.out.println ("Selected: " + e.getActionCommand());
}
} 
Painting

The following methods are early vestiges of an approach to painting and printing. They are not responsible for anything that couldn't be done with a call to paintAll() or printAll(). However, they are available if you wish to call them.

Since it is the container's responsibility to deal with painting lightweight peers, the paint() and print() methods are overridden in Java 1.1.

NOTE:

If you override paint() or print() in your containers (especially applets), call super.paint(g) or super.print(g), respectively, to make sure that lightweight components are rendered. This is a good practice even if you don't currently use any lightweight components; you don't want your code to break mysteriously if you add a lightweight component later.Peers

The container is responsible for creating and destroying all the peers of the components within it.

Miscellaneous methods