Laying out components within a container
by: Anatoliy Malyarenko
Contents of the lecture.
- A visual guide to layout managers.
- Tips on choosing the layout manager.
- How to use different layout managers.
Every content pane is initialised to use a BorderLayout. A BorderLayout places
components in up to five areas: top, bottom, left, right, and center. All extra space is placed in
the center area.
The BoxLayout class puts components in a single row or column. It respects the
components' requested maximum sizes and also lets you align components.
The CardLayout class lets you implement an area that contains different components at
different times. A CardLayout is often controlled by a combo box, with the state of the combo
box determining which panel (group of components) the CardLayout displays. An alternative
to using CardLayout is using a tabbed pane, which provides similar functionality but with a
FlowLayout is the default layout manager for every JPanel.
It simply lays out
components in a single row, starting a new row if its container isn't sufficiently wide. Both
panels in CardLayoutDemo, shown previously, use FlowLayout.
GridBagLayout is a sophisticated, flexible layout manager. It aligns components by
placing them within a grid of cells, allowing some components to span more than one cell. The
rows in the grid can have different heights, and grid columns can have different widths.
GridLayout simply makes a bunch of components equal in size and displays them in
the requested number of rows and columns.
SpringLayout is a flexible layout manager designed for use by GUI builders. It lets
you specify precise relationships between the edges of components under its control. For
example, you might define that the left edge of one component is a certain distance (which
can be dynamically calculated) from the right edge of a second component.
Setting the layout manager
A layout manager is an object that implements the LayoutManager interface and
determines the size and position of the components within a container. Although components
can provide size and alignment hints, a container's layout manager has the final say on the
size and position of the components within the container.
As a rule, the only containers whose layout managers you need to worry about are
JPanels and content panes. Each JPanel object is initialised to use a FlowLayout, unless
you specify differently when creating the JPanel.
Content panes use BorderLayout by
default. If you don't like the default layout manager that a panel or content pane uses, you're
free to change it to a different one.
You can set a panel's layout manager using the JPanel constructor. For example:
JPanel panel = new JPanel(new BorderLayout());
After a container has been created, you can set its layout manager using the setLayout
method. For example:
Container contentPane = frame.getContentPane();
Although we recommend that you use layout managers, you can perform layout without
them. By setting a container's layout property to null, you make the container use no layout
manager. With this strategy, called absolute positioning, you must specify the size and position
of every component within that container. One drawback of absolute positioning is that it
doesn't adjust well when the top-level container is resized. It also doesn't adjust well to
differences between users and systems, such as different font sizes and locales.
Adding components to a container
When you add components to a panel or content pane, the arguments you specify to
the add method depend on the layout manager that the panel or content pane is using. For
example, BorderLayout requires that you specify the area to which the component should be
added, using code like this:
The how-to section for each layout manager, described later, has details on what,
if any, arguments you need to specify to the add method. Some layout managers, such
as GridBagLayout and SpringLayout, require elaborate setup procedures. Many layout
managers, however, simply place components based on the order they were added to their
Providing size and alignment hints
Sometimes you need to customise the size hints that a component provides to its
container's layout manager, so that the component will be laid out well. You can do this by
specifying one or more of the minimum, preferred, and maximum sizes of the component.
You can invoke the component's methods for setting size hints -- setMinimumSize,
setPreferredSize, and setMaximumSize. Or you can create a subclass of the component
that overrides the appropriate getter methods -- getMinimumSize, getPreferredSize, and
getMaximumSize. Here is an example of making a component's maximum size unlimited:
Many layout managers don't pay attention to a component's requested maximum size.
However, BoxLayout and SpringLayout do.
Besides providing size hints, you can also provide alignment hints. For example, you
can specify that the top edges of two components should be aligned. You set alignment
hints either by invoking the component's setAlignmentX and setAlignmentY methods, or
by overriding the component's getAlignmentX and getAlignmentY methods. Although most
layout managers ignore alignment hints, BoxLayout honors them.
Putting space between components
Three factors influence the amount of space between visible components in a container:
The layout manager Some layout managers automatically put space between components;
others don't. Some let you specify the amount of space between components. See the
how-to page below for each layout manager for information about spacing support.
Invisible components You can create lightweight components that perform no painting, but
that can take up space in the GUI. Often, you use invisible components in containers
controlled by BoxLayout.
Empty borders No matter what the layout manager, you can affect the apparent amount
of space between components by adding empty borders to components.
candidates for empty borders are components that typically have no default border, such
as panels and labels. Some other components might not work well with borders in some
look-and-feel implementations, because of the way their painting code is implemented.
to be continued....
8631 bytes more | comments? | | Score: 0