Previous | Next | Trail Map | Creating a User Interface | 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 most cases.

To create a custom layout manager, you must create a class that implements the LayoutManager interface. Besides implementing the five methods required by LayoutManager, layout managers generally implement at least one public constructor and the toString() method. Layout managers don't actually draw Components. They simply invoke each Component's resize(), move(), and/or reshape() to set the Component's size and position.

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:



Previous | Next | Trail Map | Creating a User Interface | Laying Out Components within a Container