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
BorderLayout
orGridBagLayout
. 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
JPanel
to hold the components and using theJPanel
's defaultFlowLayout
manager.
- Scenario: You need to display a few components of the same size in rows and columns.
GridLayout
is 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. AllJPanel
objects are initialized to use aFlowLayout
. The content pane for allJApplet
objects and allJFrame
objects 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
CardLayout
manager 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
TheContainer
methods that result in calls to the container's layout manager areadd
,remove
,removeAll
,doLayout
,invalidate
,getAlignmentX
,getAlignmentY
,getPreferredSize
,getMinimumSize
, andgetMaximumSize
. Theadd
,remove
, andremoveAll
methods add and remove components from a container; you can call them at any time. ThedoLayout
method, which is called as the result of any paint request to a container or of avalidate
call on the container, requests that the container place and size itself and the components it contains; you don't call thedoLayout
method directly.If you change the size of a component by indirect means, such as changing its font, you should invoke the
invalidate
method on the component. Then you should callvalidate
on its container so thatdoLayout
will be executed.The
getAlignmentX
andgetAlignmentY
methods 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
, andgetMaximumSize
methods 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
getPreferredSize
andgetMinimumSize
methods. The values these methods return are meaningless unless the container and its components have valid peer objects. See Details of the Component Architecture for information on when peers are created.
Laying Out Components within a Container |