Java: Content Panes
Description
Before Java 2 each top-level container had only one layer. Java 2 top-level containers
(JFrame, JApplet, ...) have several layers (panes):
root, content, layered, and glass.
Programs normally reference only the content pane.
There are two programming idioms for using the content
pane: (1) using the preassigned pane (recommended), or (2) building your
own pane.
Naming convention
It is common to name the content pane content
or contentPane
.
Idiom 1: Using the existing content pane
Each container has a preconstructed content pane
of class Container
. You can get this
pane and add the components to it.
For example,
class MyWindow extends JFrame {
. . .
MyWindow() { // constructor
Container content = this.getContentPane();
content.add(...);
content.add(...);
. . .
All JFrames already have a content pane, so there's no need to
create a new one, just get the existing pane.
Idiom 2: Creating your own content pane
It isn't uncommon to create a panel that has the GUI components
on it, then use this panel for the content pane. For example,
class MyWindow extends JFrame {
. . .
MyWindow() { // constructor
JPanel content = new JPanel();
content.add(...);
content.add(...);
. . .
setContentPane(content);