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 every 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 AWT-provided layout managers 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, you should feel free to use layout managers contributed to the net, such as PackerLayout.
- Scenario: You need to display a component in as much space as it can get.
- Consider using BorderLayout or GridBagLayout. If you use BorderLayout, 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 that
fill=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 a GridLayout.
- Scenario: You need to display a few components in a compact row at their natural size.
- Consider using a Panel to hold the components and using the Panel's default FlowLayout manager.
- Scenario: You need to display a few same-sized components in rows and/or columns.
- GridLayout is perfect for this.
How to Create a Layout Manager and Associate It with a Container
Every container has a default layout manager associated with it. All Panels (including Applets) are initialized to use a FlowLayout. All Windows (except special-purpose ones like FileDialog) are initialized to use a BorderLayout.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 non-default layout manager, you need to create an instance of the desired layout manager class and tell the Container to use it. Below is some typical code that does this. This code creates a CardLayout manager and sets it up as the layout manager for a Container.
aContainer.setLayout(new CardLayout());Rules of Thumb for Using Layout Managers
The Container methods that result in calls to the Container's layout manager areadd()
,remove()
,removeAll()
,layout()
,preferredSize()
, andminimumSize()
. Theadd()
,remove()
, andremoveAll()
methods add and remove Components from a Container; you can call them at any time. Thelayout()
method, which is called as the result of any paint request to a Container, requests that the Container place and size itself and the Components it contains; you don't usually call it directly. ThepreferredSize()
andminimumSize()
methods return the Container's ideal size and minimum size, respectively. The values returned are just hints; they have no effect unless your program enforces these sizes.You must take special care when calling a Container's
preferredSize()
andminimumSize()
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 |