Robotics C++ Physics II AP Physics B Electronics Java Astronomy Other Courses Summer Session  

Layout Managers

 

 

The Basic Idea

Swing Containers

AWT Containers

Layout Managers

  Sample Code

Exercises

 

 

 

The Basic Idea                                      

 
In order to create a good graphical user interface, components must be arranged within some kind of container. A container is a component that can contain other components. All containers inherit from the java.awt.Container base class, which itself inherits from java.awt.Component.
 
Main application windows and dialog boxes are commonly used containers. Each provides a window within which GUI components can be arranged to create a user interface. A graphical application does not usually arrange all its components directly within a window or dialog box, however. Instead, an application typically uses containers nested within other containers. For example, a dialog box that contains two columns of text input fields above a row of push buttons might use three separate containers, one for each column of text fields and one for the row of push buttons.
 
Some kinds of containers display their children in specific ways, while others have restrictions on the number or type of components they can display. Some are generic, so they can contain any number of children. Etc.

 

AWT Containers

 

Container

Description

 
 
Applet
This subclass of Panel is part of the java.applet package. It is the base class for all applets.
Container
The base class from which all containers inherit
Dialog
A window suitable for dialog boxes
Frame
A window suitable for use as the main window of an application.
Panel
Generic container used to create nested layouts
ScrollPane
A container that contains a single child and allows that child to be scrolled vertically and horizontally
Window
A container with no title bar or other decoration, suitable for pop-up menus and similar uses
 

 

Swing Containers

 

Container

Description

 
 
Box
General-purpose container that arranges children using BoxLayout layout manager
JApplet
A java.applet.Applet subclass that contains a JRootPane to add Swing features, such as support for menu bars to applets
JDesttopPane
A container for JInternalFrame components; simulates the operation of a desktop within a single window.
JDialog
The container used to display dialog boxes in Swing
JFrame
The container used for top-level windows in Swing.
JInternalFrame
A nested window container. Behaves like a JFrame and displays a Title Bar and resize handles but is not an independent window and is constrained to appear within bounds of its parent container.
JLayeredPane
Container that allows its children to overlap and manages the stacking order of those children
JPanel
A generic container for grouping Swing components. Typically used with an appropriate layout manager
JRootPane
A complex container used internally by JApplet, JDialog, JFrame and others.
JScrollPane
Container that allows a single child component to be scrolled horizontally or vertically.
JSplitPane
Container that displays two children by splitting itself horizontally or vertically
JTabbedPane
Displays one child at a time, allowing the user to select the currently displayed child by clicking on tabs.
JViewPort
Fixed-size container that displays a portion of a single larger child.
JWindow
Top-level Swing window that does not display a titlebar, resize handles, or any other decorations
 
Layout Managers (Scenarios)

 

Scenarios          
Scenario:
 
You need to display a component in as much space as it can get.
Consider using BorderLayout or GridBagLayout. If you use BorderLayout, you'll need to put the space-hungry component in the center. With GridBagLayout, you'll need to set the constraints for the component so that fill = GridBayConstraints.BOTH. Another possibility is to useBoxLayout, making the space-hungry component specify very large preferred and maximum sizes.
 
Scenario:
 
You need to display a few components in a compact row at their natural size.
Consider using a JPanel to group the components and using either the JPanel's default FlowLayout manager or the BoxLayout manager.
 
Scenario:
 
You need to display a few components of the same size in rows and columns.
GridLayout is perfect for this.
 
Scenario:
 
You need to display a few components in a row or column, possibly with varying amounts of space between them, custom alignment, or custom component sizes. BoxLayout is perfect for this.
 
Scenario:
 
You have a complex layout with many components.
Consider either using GridBagLayout or grouping the components into one or mor JPanels  to simplify layout. Each JPanel might use a different layout manager.

 

 

 Manager

Description

 
 

BorderLayout

Lays out a maximum of five components: one along each of the four borders of the container and one in the center. When using this layout manager, you must add components to the container using a two-argument version of the add() method. The constraint argument should be one of the strings "North", "East", "South", "West", or "Center".
CardLayout
Makes each component as large as the container and displays only one at a time. Various methods change the currently displayed component

FlowLayout

Arranges components like words on a page; from left to right in rows and then top to bottom as each row fills. Rows may be left, center, or right justified.

GridBagLayout

A flexible manager that arranges components in a grid with variable-sized cells. Allows control over the way each component is resized when the container changes size.
GridLayout
Makes all components the same size and arranges them in a grid of specified dimensions. Parameters are number rows, number cols, int hgap, int vgap. If either the number of rows or number of columns is set to 0, its value is computed from the other dimension and the total number of components. Simpler than GridBagLayout.
BoxLayout
Manger used by the Box container. Arranges children either in a row or a column.
OverlayLayout
Infrequently used - not covered here
ScrollPaneLayout
Specialized layout manager used by JScrollPane.
ViewportLayout
Specialized layout manager used by JViewport.
 
 

BorderLayout

FlowLayout

 

GridBagLayout