Laying Out Components within a Container |
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'sresize()
,move()
, and/orreshape()
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:
Laying Out Components within a Container |