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
andBoxLayout
are flexible enough to work in many cases. You can also find layout managers from other sources, such as from the Internet. Finally, you can simplify layout by grouping components into containers such as invisible panels.
To create a custom layout manager, you must create a class that implements the
LayoutManager
interface. You can either implement it directly, or implement its subinterface,LayoutManager2
.Every layout manager must implement at least the following five methods, which are required by the
LayoutManager
interface:
void addLayoutComponent(String, Component)
- Called by the
Container
add
methods. Layout managers that don't associate strings with their components generally do nothing in this method.
void removeLayoutComponent(Component)
- Called by the
Container
remove
andremoveAll
methods. Many layout managers do nothing in this method, relying instead on querying the container for its components, using theContainer
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 container, assuming that the components it contains will be at or above their preferred sizes. This method must take into account the container's internal borders, which are returned by thegetInsets
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 container, assuming that the components it contains will be at or above their minimum sizes. This method must take into account the container's internal borders, which are returned by thegetInsets
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'sresize
,move
, andreshape
methods to set the component's size and position. This method must take into account the container's internal borders, which are returned by thegetInsets
method. You can't assume that thepreferredLayoutSize
orminimumLayoutSize
method will be called beforelayoutContainer
is called.Besides implementing the preceding five methods, layout managers generally implement at least one public constructor and the
toString
method.If you wish to support component constraints, maximum sizes, or alignment, then your layout manager should implement the
LayoutManager2
interface. That interface adds five methods to those required byLayoutManager
:For more information about these methods, see the
addLayoutComponent(Component, Object)
getLayoutAlignmentX(Container)
getLayoutAlignmentY(Container)
invalidateLayout(Container)
maximumLayoutSize(Container)
LayoutManager2
API documentation. Also see the source code forBoxLayout
, to see how it implements theLayoutManager2
interface.When implementing a layout manager, you might want to use
SizeRequirements
objects to help you determine the size and position of components. See the source code forBoxLayout
for an example of usingSizeRequirements
.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 ofDiagonalLayout
in action:
This is a picture of the applet's GUI. To run the applet, click the picture. The applet will appear in a new browser window.
Laying Out Components Within a Container |