Java AWT Containers

Java AWT Containers

After knowing what a component is, let us go to the list of containers Java supports. For this, the container hierarchy is separated from the earlier component hierarchy and displayed here.

Infact, the Container is a component as it is a subclass of Component class. It can use all the methods of Component class and an extra added feature is components can be added to this component (container).

  1. Any class subclass of a Container class is known as a container.
  2. Only components can be added to container.
  3. Every container comes with a default layout manager that can be changed explicitly with setLayout() method.

Java AWT Containers

As you can observe from the above hierarchy, Container is the super class of all Java containers.

Following is the class signature

public class Container extends Component

Following are the important methods of the Container class.

  1. add(): This method is overloaded with which a component can be added to the container.
  2. invalidate(): Used to invalidate the present set up of components in the container.
  3. validate(): Used to revalidate the current set up of components after calling invalidate().

The important containers used very often are applets, frames and panels. Rare are dialog boxes. Panels work very differently. Panel behaves both as component and container. As component panel can be added to a container and as container we can add components to panel.

Leave a Reply

Your email address will not be published. Required fields are marked *