Wednesday 2 March 2016

Java Containers

The Container class is an extended type of Component that maintains a list of child components and helps to group them. The Container causes its children to be displayed and arranges them on the screen according to a particular layout strategy. Because a Container is also a Component, it can be placed alongside other Component objects in other Containers in a hierarchical fashion, as shown in the figure below. Our HelloComponent3 class is a kind of Container (by virtue of the JComponent class) and can therefore hold and manage other Java components and containers, such as buttons, sliders, text fields, and panels.

In the figure above, the italicized items are Components, and the bold items are Containers. The keypad is implemented as a container object that manages a number of keys. The keypad itself is contained in the GizmoTool container object. Since JComponent descends from Container, it can be both a component and a container. In fact, we’ve already used it in this capacity in the HelloComponent3 example. It does its own drawing and handles events, just like a component, but it also contains a button, just like a container.

0 comments:

Post a Comment