Ask Questions?

View Latest Questions



Posted on: July 22, 2006 at 12:00 AM
Top-level Containers - window, applet, dialog, ...

Java Notes


Top-level Containers - window, applet, dialog, ...

All elements of a user interface must be added to the content pane of a top-level container in Java 2. You can also set the content pane to a new JPanel (but not a split, tabbed, or scroll pane) with myFrame.setContentPane(myPanel), adding components to myPanel.

Use JFrame - Window for a window, and JApplet for an applet.

Another type of top-level container is a dialog, which can be created with JDialog, but it is usually easier to use Dialogs.

You will use these three types of top-level containers:

  • Windows (JFrame) have a title bar with close and iconifying buttons.
  • Dialogs (JOptionPane, ProgressMonitor, JDialog, JFileChooser, JColorChooser) can easily be created. To get a print dialog you should use methods in the Toolkit class.
  • Applets (JApplet).

Intermediate Containers

To group components together put them on a JPanel - Container, where you can set the layout, background, and borders. You can also use a JPanel for graphics, but then don't also use it as a component container.

There are other specialized containers that act more like components, for example, JScrollPane, ... .

After you have added your components to a panel (eg, content), set the content pane of the top-level container (eg, myApplet) to this panel with myApplet.setContentPane(content);.

Here is a list of the intermediate containers, though you may never use more than JPanel.

  • Panels (JPanel) are used to group components, or provide a drawing area.
  • Scroll panes (JScrollPane) for areas that are too big to display entirely. This will have vertical and horizontal scrollbars to move in.
  • Split panes (JSplitPane) allow the user to change the relative size of two panes.
  • Tabbed panes (JTabbedPane) For displaying panes with tabs.
  • Tool bars (JToolBar) for displaying toolbars.
  • Other: JInternalFrame (if you need a window inside a window).


The way the components are arranged in a container and the way the components are rearranged when a container is resized is determined by the container's Layouts.

Copyleft 2003 Fred Swartz MIT License