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 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
andremoveAll
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 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 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 theContainer
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 theContainer
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'sresize
,move
, andreshape
methods to set the component's size and position. This method must take into account the parent's internal borders, which are returned by theContainer
getInsets
method. You can't assume that thepreferredLayoutSize
orminimumLayoutSize
method will be called beforelayoutContainer
is called.Besides implementing the five methods required by
LayoutManager
, layout managers generally implement at least one public constructor and thetoString method. Instead of implementing
LayoutManager
directly, some layout managers implement theLayoutManager2
interface. TheLayoutManager2
interface extendsLayoutManager
by adding methods that take components' maximum sizes and alignments into account, a more general form of theaddLayoutComponent
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 viewCustom.html
, specifyingswing.jar
in the Applet Viewer's class path. For more information about running applets, refer to About Our Examples.
Laying Out Components within a Container |