|
|
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,GridBagLayoutis 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
LayoutManagerinterface.LayoutManagerrequires its adherents to implement five methods:
void addLayoutComponent(String, Component)- Called only by the
Containeradd(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
ContainerremoveandremoveAllmethods. 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 theContainergetComponentsmethod.
Dimension preferredLayoutSize(Container)- Called by the
ContainergetPreferredSizemethod, 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 theContainergetInsetsmethod.
Dimension minimumLayoutSize(Container)- Called by the
ContainergetMinimumSizemethod, 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 theContainergetInsetsmethod.
void layoutContainer(Container)- Called when the container is first displayed, and each time its size changes. A layout manager's
layoutContainermethod doesn't actually draw components. It simply invokes each component'sresize,move, andreshapemethods to set the component's size and position. This method must take into account the parent's internal borders, which are returned by theContainergetInsetsmethod. You can't assume that thepreferredLayoutSizeorminimumLayoutSizemethod will be called beforelayoutContaineris called.Besides implementing the five methods required by
LayoutManager, layout managers generally implement at least one public constructor and thetoString method. Instead of implementingLayoutManagerdirectly, some layout managers implement theLayoutManager2interface. The
LayoutManager2interface extendsLayoutManagerby adding methods that take components' maximum sizes and alignments into account, a more general form of theaddLayoutComponentmethod, 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
DiagonalLayoutin 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.jarin the Applet Viewer's class path. For more information about running applets, refer to About Our Examples.
|
|
Laying Out Components within a Container |