|
|
Laying Out Components Within a Container |
Unless you explicitly tell a container not to use a layout manager, the container 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 Java platform have different strengths and weaknesses. This section discusses some common layout scenarios and which layout managers might work for each scenario. If none of these layout managers is right for your situation, feel free to use other layout managers that you write or find.
- 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. WithGridBagLayout, you'll need to set the constraints for the component so thatfill=GridBagConstraints.BOTH. Another possibility is to useBoxLayout, making the space-hungry component specify very large preferred and maximum sizes.
- 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 either theJPanel's defaultFlowLayoutmanager or theBoxLayoutmanager.
- Scenario: You need to display a few components of the same size in rows and columns.
GridLayoutis perfect for this.
- Scenario: You need to display a few components in a row or column, possibly with varying amounts of space between them, custom alignment, or custom component sizes.
BoxLayoutis perfect for this.
- Scenario: You have a complex layout with many components.
- Consider either using
GridBagLayoutor grouping the components into one or moreJPanels to simplify layout. EachJPanelmight use a different layout manager.How to Create a Layout Manager and Associate It with a Container
As we mentioned in Layout Management, by convention each container has a layout manager. All
JPanelobjects are initialized to use aFlowLayout. The content pane for allJApplet,JDialog, andJFrameobjects is initialized to use aBorderLayout. Other Swing containers tend to have very specialized layout managers or, as in the case ofJLayeredPane, no layout manager at all. [PENDING: reword other layout stuff to reflect this.]
If you want to use a container's default layout manager, you don't have to do a thing. The constructor for the 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. Normally, you would do this only for
JPanels and content panes. The following statement creates aBorderLayoutmanager and sets it up as the layout manager for a panel.Here is an example of making aaJPanel.setLayout(new BorderLayout());FlowLayoutobject the layout manager for a applet's content pane:For examples of creating each layout manager, see the how-to section for the particular layout manager.//In a JApplet subclass: Container contentPane = getContentPane(); contentPane.setLayout(new FlowLayout());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. [PENDING: check with aim]If you change the size of a component, even by indirect means such as changing its font, the component should automatically resize and repaint itself. If that doesn't happen for some reason, then you should invoke the
revalidatemethod on the component. This request is passed up the containment hierarchy until it encounters a container, such as a scroll pane or top-level container, that shouldn't be affected by the component's resizing. (This is determined by calling the container'sisValidateRootmethod.) The container is then laid out, which has the effect of adjusting the revalidated component's size and the size of all affected components. After callingrevalidateon a component, you should then invokerepainton it. [PENDING: check]The
getAlignmentXandgetAlignmentYmethods are called by layout managers that try to align groups of components.BoxLayoutis the only layout manager we discuss that calls these methods. [PENDING: OverlayLayout also calls these methods and implements LayoutManager2; be careful about promises about "Java platform"; "layout managers discussed in this trail/book" is more accurate.]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.
|
|
Laying Out Components Within a Container |