Layout : Java Glossary


a method that fine tunes the sizes and positions of a group of graphical elements from a few rough hints on how you want the layout done. You can roll your own layout manager, or use one of the standard ones.
Components in the Layout Table of Possible Layout Managers
What Layout Managers Do Gotchas
Null Layouts Learning More
Nested Containers Links

Components in the Layout

Each component has four sizes (height,width).

These methods often return 0,0 as the size. The reason is, they rely on the underlying GUI (Graphic User Interface) to compute the size and if there in no peer object yet, there is no hook to ask the native GUI. See addNotify.

What Layout Managers Do

When you call Container.validate(), the Container’s layout manager decides how big and where each Component should be, taking hints from getPreferredSize, getMinimumSize and getMaximumSize of each Component and does a setBounds(x, y, width, height) on each component in the Container, which sets the component’s actual size and location. The routine in the LayoutManager that actually assigns coordinates and sizes to each component is called: However, the container as a whole also needs a getMinimumSize(), getMaximumSize() and getPreferredSize() method too, since it acts a component in an enclosing container. So the layout manager also needs to provide generic routines for computing the desired size of the container by totalling up the sizes of the contained components taking into consideration how they are packed in. These routines are indirectly used by another higher level layout manager. The routines that compute the sizes of an entire container are called:

Null Layouts

If you want to do absolute pixel positioning, without a layout manager, you must do Container.setLayout( null ) to turn off the default FlowLayout. This is trickier than you might imagine. See the gotchas. You are better off writing your own custom LayoutManager. With a null layout manager, it is then up to you to figure out how big each Component in the Container should be, where it should be and tell the Components with setSize, setLocation or setBounds.

You should almost never use null layouts and setLocation. This is a last ditch emergency method when all layout managers have failed to give you decent positioning and it would be impossible to write a layout manager with enough common sense.

When you do use manual layout you must do several things:

Nested Containers

A Container may live inside another Container. Either container may or may not have a layout manager. In any case, the inner Container behaves like an ordinary Component when relating to the outer Container’s layout. You have to keep your wits about you, keeping the code for Container as Container separate from Container as Component in a bigger Container. A Container as Component has a getMinimumSize, getMaximumSize, getPreferredSize, getSize and setSize method.

Table of Possible Layout Managers

Possible Layout Managers
Layout Purpose
AbsoluteLayout Netbeans absolute position layout.
BorderLayout click to see screenshot Place components on a simple north, south, east, west, center grid.
Oracle’s Javadoc on BorderLayout class : available:
BoxLayout click to see screenshot Place components in a single row or column. Designed to work with glue and strut objects of the Box class to control where extra space goes. Aligns based on the  JComponent.getAlignmentX and JComponent.getAlignmentY properties of the JComponents in the Container. To control alignment use  JComponent.setAlignmentX ( 0.0F ) for left aligned, setAlignmentX( .5F ) for centered and setAlignmentX( 1.0F ) for right aligned. Only available in Swing.
Oracle’s Javadoc on BoxLayout class : available:
CardLayout click to see screenshot Allows you to flip through several panels, where only one is visible at a time.
Oracle’s Javadoc on CardLayout class : available:
DesignGridLayout click to see screenshot Uses Patrizia Nanni’s canonical grids.
EasyLayout click to see screenshot Stripped down GridBagLayout. Has just weights and grid numbers.
ExplicitLayout Allows non-rectangular layouts. By Zooki Technology.
FillLayout SWT (Standard Widget Toolkit). lays out equal-sized widgets in a single row or column.
FlowLayout click to see screenshot Simple flow left to right, top to bottom stream of components. This is a very rough and ready, quick and dirty layout for prototypes. If you want fine control and professional looks, try another.
Oracle’s Javadoc on FlowLayout class : available:
FormLayout Karsten Lentzsch’s free, open source LayoutManager. Karsten is the Leonardo da Vinci of layout designers. His layouts get every last picky detail correct, including working with larger-than-expected fonts. Even if you don’t use his layouts, read what he has to say about GUI design. version 1.2 is coming soon which has layout variables for meta design constants. Last revised/verified: 2008-04-04 For example, 85dlu for all label column widths, 100dlu for the minimum table height, etc. See scalable layouts for a discussion of the problem of scaling layouts.
GridBagLayout click to see screenshot Oracle’s mother of all layout managers. It is complicated and powerful. To cover it requires a separate entry. Place components on a rough n x m grid. Some components may take up multiple cells. Fine tuning of whitespace around each component and alignment of each component within its grid cell. Requires a lot of tweaking to make it look right. The cells are not necessarily the same size. They adjust to fit the components, based on various hints you give.
GridLayout click to see screenshot Layout components in a regular n × m grid. All cells are the same width and height.
Oracle’s Javadoc on GridLayout class : available:
GridLayout SWT SWT. lays out widgets in a grid FormLayout
GroupLayout Part of Netbeans/Matisse. It is not bundled with the JDK (Java Development Kit). It backs up the visual layout abilities of Matisse.
HIGLayout Similar to TableLayout, by Daniel Michelik
JTabbedPane JTabbedPane is a JComponent, not a LayoutManager, but it behaves much like a CardLayout with tabs.
JSplitPane JSplitPane is a JComponent, not a Layout, but it behaves much like one with two panels and a moveable boundary between them.
LabeledGridBagLayout Like a GridBagLayout that automatically labels fields with their corresponding field names. Requires use of smart components that know their field labels. Part of the package.
Matisse: aka Gui Builder A new NetBeans scheme for building layouts interactively with a backing set of layout managers. It is still in alpha test.
MigLayout Free. Intended to be a universal layout manager for all possible applications. This is RedGrittyBrick’s favourite layout manager. Has tools for writing resolution-independent code.
Packer A front end to the GridBagLayout using a fluent interface to cut down the number of positional parameters. Not to be confused with PackerLayout.
PackerLayout A Java version of the Tcl/Tk layout manager. Some of the GridBag pad/expand abilities.
RowLayout SWT. lays out widgets in a row or rows, with fill, wrap and spacing options. ScalableLayout Zoom the Swing UI (User Interface) (like CTRL+/CTRL- in Firefox for web pages)
SpringLayout click to see screenshot Uses a model of structs and springs to control the sizes and positions of boxes. Only available in Swing.
Oracle’s Javadoc on SpringLayout class : available:
StarLayout Arranges objects in star shaped patterns. You place components by giving the angle and radius from the centre of the container.
TableLayout A simplified GridBagLayout. You explicitly control the relative sizes of each row and column. By Daniel Barbalace and the ClearThought TableLayout Project.
XYLayout Borland absolute position layout.

NetBeans and JBuilder have GUI layout builders. Radical is a stand-alone GUI Builder.


Learning More

Oracle’s Javadoc on LayoutManager class : available:

This page is posted
on the web at:

Optional Replicator mirror
on local hard disk J:

Canadian Mind Products
Please the feedback from other visitors, or your own feedback about the site.
Contact Roedy. Please feel free to link to this page without explicit permission.

Your face IP:[]
You are visitor number