The Root Pane

The JRootPane Class

JRootPane is a special container that extends JComponent and is used by many of the other Swing containers. It’s quite different from most containers you’re probably used to using. The first thing to understand about JRootPane is that it contains a fixed set of components: a Component called the glass pane and a JLayeredPane called, logically enough, the layered pane. Furthermore, the layered pane contains two more components: a JMenuBar and a Container called the content pane.

import javax.swing.*;
import java.awt.*;

public class RootExample {
	public static void main(String[] args) {
		JFrame f = new JFrame();
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		JRootPane root = f.getRootPane(); // XXX Pay attention to these
		Container content = root.getContentPane(); // XXX lines. They are
		content.add(new JButton("Hello")); // XXX explained below.
		f.pack();
		f.setVisible(true);
	}
}