|
|
Laying Out Components within a Container |
Unless you explicitly tell a container not to use a layout manager, it is associated with its very own instance of a layout manager. This layout manager is automatically consulted each time the container might need to change its appearance. Most layout managers don't require programs to directly call their methods.How to Choose a Layout Manager
The layout managers provided by the AWT have different strengths and weaknesses. This section discusses some common layout scenarios and which AWT layout managers might work for each scenario. If none of the AWT layout managers is right for your situation, feel free to use other layout managers, such as the freely availablePackerLayout
- Scenario: You need to display a component in as much space as it can get.
- Consider using
BorderLayoutorGridBagLayout. If you useBorderLayout, you'll need to put the space-hungry component in the center. With GridBagLayout, you'll need to set the constraints for the component so thatfill=GridBagConstraints.BOTH. Or, if you don't mind every other component in the same container being just as large as your space-hungry component, you can use aGridLayout.
- Scenario: You need to display a few components in a compact row at their natural size.
- Consider using a
JPanelto hold the components and using theJPanel's defaultFlowLayoutmanager.
- Scenario: You need to display a few components of the same size in rows and columns.
GridLayoutis perfect for this.
How to Create a Layout Manager and Associate It with a Container
Each container has a default layout manager associated with it. AllJPanelobjects are initialized to use aFlowLayout. The content pane for allJAppletobjects and allJFrameobjects are initialized to use aBorderLayout.If you want to use a container's default layout manager, you don't have to do a thing. The constructor for each container creates a layout manager instance and initializes the container to use it.
To use a layout manager other than the default layout manager, you must create an instance of the desired layout manager class and tell the container to use it. The following statement creates a
CardLayoutmanager and sets it up as the layout manager for a container.[PENDING: add a new section that reflects how to change the layout manager for a container with a content pane.]aContainer.setLayout(new CardLayout());Rules of Thumb for Using Layout Managers
TheContainermethods that result in calls to the container's layout manager areadd,remove,removeAll,doLayout,invalidate,getAlignmentX,getAlignmentY,getPreferredSize,getMinimumSize, andgetMaximumSize. Theadd,remove, andremoveAllmethods add and remove components from a container; you can call them at any time. ThedoLayoutmethod, which is called as the result of any paint request to a container or of avalidatecall on the container, requests that the container place and size itself and the components it contains; you don't call thedoLayoutmethod directly.If you change the size of a component by indirect means, such as changing its font, you should invoke the
invalidatemethod on the component. Then you should callvalidateon its container so thatdoLayoutwill be executed.The
getAlignmentXandgetAlignmentYmethods are called by layout managers that try to align groups of components. None of the layout managers in the 1.1 JDK calls these methods. [PENDING: BoxLayout calls this, right?]The
getPreferredSize,getMinimumSize, andgetMaximumSizemethods return the container's ideal, minimum, and maximum sizes, respectively. The values returned are just hints; a layout manager can ignore them.Take special care when calling a container's
getPreferredSizeandgetMinimumSizemethods. The values these methods return are meaningless unless the container and its components have valid peer objects. See Details of the Component Architecturefor information on when peers are created.
|
|
Laying Out Components within a Container |