Java Reference
In-Depth Information
ipadx
and
ipady
specify the internal padding within the component.
ipadx
specifies the extra
space to the right and left of the component (so the minimum width increases by 2×
ipadx
pixels).
ipady
specifies the extra space above and below the component (so the minimum height increases
by 2×
ipady
pixels). The difference between
insets
(external padding) and
ipadx
/
ipady
(internal
padding) can be confusing. The
insets
don't add space to the component itself; they are
external to the component.
ipadx
and
ipady
change the component's minimum size, so they
do add space to the component itself.
Weight
weightx
and
weighty
describe how to distribute any additional space within the container.
They allow you to control how components grow (or shrink) when the user resizes the
container, or the container is just bigger to start.
If
weightx
is
0.0
, the component won't get any additional space available in its row. If one
or more components in a row have a positive
weightx
, any extra space is distributed propor-
tionally between them. For example, if one component has a
weightx
value of
1.0
and the
others are all
0.0
, the one component will get all the additional space. If four components in a
row each has a
weightx
value of
1.0
, and the other components have
weightx
values of
0.0
, the
four components each get one quarter of the additional space.
weighty
behaves similarly to
weightx
, but in the other direction. Because
weightx
and
weighty
control the distribution of
extra space in any row or column, setting either for one component may affect the position of
the other components.
CardLayout Class
The
CardLayout
layout manager is significantly different from the other layout managers.
Whereas the other layout managers attempt to display all the components within the container
at once, a
CardLayout
displays only one component at a time. That component can be a component
or container, where the latter lets you see multiple components laid out based on the layout
manager of the embedded container.
Now that the
JTabbedPane
component (described in the next chapter) is available,
CardLayout
is rarely used.
BoxLayout Class
Swing's
BoxLayout
manager allows you to position components in either a horizontal row or a
vertical column within your own container. In addition to using
BoxLayout
within your own
container, the
Box
class (described in the next chapter) offers a container that uses
BoxLayout
as
its default layout manager.
The benefit of using
BoxLayout
over something like
FlowLayout
or
GridLayout
is that
BoxLayout
works to honor each component's x and y alignment properties as well as its maximum size.
And
BoxLayout
is much easier to use than
GridBagLayout
. Figure 10-8 demonstrates
BoxLayout
in action. Previously, you would have needed to figure out the necessary layout constraints to
get
GridBagLayout
to behave like this.