|
|
Start of Tutorial > Start of Trail > Start of Lesson | Search |
As a rule, the only time you have to think about layout managers is when you create aJPanelor add components to a content pane
. If you don't like the default layout manager that a panel or content pane uses, then you can change it to a different one. When you add components to a panel or content pane, the arguments you specify to the
addmethod depend on the layout manager that the panel or content pane is using.The other Swing containers are more specialized and tend to hide the details of which layout manager (if any) they use. For example, a scroll pane
relies on a layout manager named
ScrollPaneLayout, but you don't need to know that to use a scroll pane. Most Swing containers provide API that you can use instead of theaddmethod. For example, instead of adding a component directly to a scroll pane (or, actually, to its viewport), you either specify the component in theJScrollPaneconstructor or usesetViewportView. For information about how to add components to a specific container, see the how-to page for the container. You can find the how-to pages using A Visual Index to the Swing Components.
Layout managers have different strengths and weaknesses. This section discusses some common layout scenarios and which layout managers might work for each scenario. If none of the layout managers we discuss 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 group 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.
As mentioned in Layout Management, each container either has a layout manager or uses absolute positioning. All
JPanelobjects are initialized to use aFlowLayout. Content panes (the main containers inJApplet,JDialog, andJFrameobjects) useBorderLayout, by default.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. The following statement creates a
BorderLayoutmanager 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 an 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());
A container's layout manager is automatically consulted each time the container might need to change its appearance. For example, the layout manager is consulted whenever the user adds a component to a container. Most layout managers don't require programs to directly call their methods.Only a few component methods result in a layout manager actually performing layout: the
JComponentrevalidatemethod, theWindowpackmethod, and theWindowshowandsetVisiblemethods. Other methods that result in calls to a container's layout manager (but don't trigger a new layout) include theContaineradd,remove,removeAll,getAlignmentX,getAlignmentY,getPreferredSize,getMinimumSize, andgetMaximumSizemethods.If you change the size of a component, even indirectly by changing its font, for example, the component should automatically resize and repaint itself. If that doesn't happen -- because you made the change to the component's data model instead of the component, for example -- you should invoke the
revalidatemethod on the component.When you invoke
revalidateon a component, a 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 calling
revalidateon a component, you can invokerepainton it to make the change appear onscreen. Bothrevalidateandrepaintare thread-safe-- you needn't invoke them from the event-dispatching thread.
|
|
Start of Tutorial > Start of Trail > Start of Lesson | Search |