Previous | Next | Trail Map | Creating a User Interface (with Swing) | Laying Out Components within a Container

Creating a Custom Layout Manager


Note: Before you start creating a custom layout manager, make sure that no existing layout manager will work. In particular, GridBagLayout is flexible enough to work in many cases. You can also find layout managers from other sources, such as from the Internet.

To create a custom layout manager, you must create a class that implements the LayoutManager interface. LayoutManager requires its adherents to implement five methods:

void addLayoutComponent(String, Component)
Called only by the Container add(String, Component) method. Layout managers that don't require that their components have names generally do nothing in this method.

void removeLayoutComponent(Component)
Called by the Containerremove and removeAll methods. Layout managers that don't require that their components have names generally do nothing in this method, since they can query the container for its components using the Container getComponents method.

Dimension preferredLayoutSize(Container)
Called by the Container getPreferredSize method, which is itself called under a variety of circumstances. This method should calculate and return the ideal size of the parent, assuming that the components it contains will be at or above their preferred sizes. This method must take into account the parent's internal borders, which are returned by the Container getInsets method.

Dimension minimumLayoutSize(Container)
Called by the Container getMinimumSize method, which is itself called under a variety of circumstances. This method should calculate and return the minimum size of the parent, assuming that the components it contains will be at or above their minimum sizes. This method must take into account the parent's internal borders, which are returned by the Container getInsets method.

void layoutContainer(Container)
Called when the container is first displayed, and each time its size changes. A layout manager's layoutContainer method doesn't actually draw components. It simply invokes each component's resize, move, and reshape methods to set the component's size and position. This method must take into account the parent's internal borders, which are returned by the Container getInsets method. You can't assume that the preferredLayoutSize or minimumLayoutSize method will be called before layoutContainer is called.

Besides implementing the five methods required by LayoutManager, layout managers generally implement at least one public constructor and the toString method. Instead of implementing LayoutManager directly, some layout managers implement the LayoutManager2(in the API reference documentation) interface. The LayoutManager2 interface extends LayoutManager by adding methods that take components' maximum sizes and alignments into account, a more general form of the addLayoutComponent method, and a method that tells the layout manager to discard any cached layout information.

Here's source code for a custom layout manager named DiagonalLayout. It lays out components diagonally, from left to right, with one component per row.

Here's an example of DiagonalLayout in action:


Your browser can't run 1.0 Java applets, so here's a picture of the window the program brings up:


Note: Because the preceding applet runs using Java Plug-in 1.1.1, it is a Swing 1.0.3 version of the applet. To run the Swing 1.1 Beta 3 version of the applet, you can use the JDK Applet Viewer to view Custom.html, specifying swing.jar in the Applet Viewer's class path. For more information about running applets, refer to About Our Examples.


Previous | Next | Trail Map | Creating a User Interface (with Swing) | Laying Out Components within a Container