Java Reference
In-Depth Information
You saw in the Try It Out how to add glue and rigid areas, i.e. invisible components. You'll next
consider these in a little more detail, but not before these general tips:
Note how the alignment of a component can be set to a float value, meaning that you can
also specify intermediate alignments between all the way to the left (0F), all the way to the
right (1F), or a center alignment (0.5F). Try playing with this in the Try It Out.
Note how the minimum and maximum sizes of the panels were set to be equal to the pre-
ferred size. If these are different values, or if you don't specify them all, the panel will be
more flexible to stretch and fill any available space. When you're having trouble with compo-
nents misbehaving in a BoxLayout , try checking their sizes.
Now, concerning glue and rigid areas, there are three types of invisible components that can
help you add space between components in a BoxLayout :
A rigid area, created with Box.createRigidArea(Dimension d) : Use this when you
want a fixed‐size space between two components.
Glue, created with Box.createHorizontalGlue() or Box.createVerticalGlue() :
Use this to specify where excess space in a layout should go when resizing a window.
Think of this component as some invisible elastic stretching glue between components.
It still allows components to stick closely together, but will expand when stretched.
A custom filler, created with new Box.Filler(Dimension minimumSize,
Dimension preferredSize, Dimension maximumSize) : Use this to specify a com-
ponent with whatever minimum, preferred, and maximum sizes you want. This is
equal to creating some invisible JPanel with set sizes.
A strut: Well, you read that there are three types of invisible components, but in
actuality, there are four. A strut also provides a way to add filler, but the rigid area
provides the same functionality and avoids some sizing issues in some cases, so that it
is always better to use a rigid area instead of a strut.
Finally, you can also add an invisible border to components to push them apart.
We recommend against this solution, however, because you will end up adding glue
or rigid areas anyway once you want to add a real border to your components.
grouplayout and springlayout
The next two layout managers are placed in the same section, as they both offer a huge amount of
flexibility, but are also incredibly hard to use. The reason behind this is that they were never meant
to be used in a manual manner anyway.
The GroupLayout layout manager was originally developed to be used in combination with graphi-
cal GUI designers, such as the one provided in Netbeans (an IDE just like Eclipse). GroupLayout can
still be used in a manual manner; the basic thing to know is that components are grouped hierarchi-
cally and groups are laid out either sequentially (one after another) or parallel (next to each other).
On the other hand, the SpringLayout layout manager is even more complex, with the ability to
emulate almost all features of the other layout managers. You can find online references showing
you how to construct a SpringLayout by hand.
This topic doesn't cover either of these layout managers in detail. This is because the other lay-
out managers you have seen, GridBagLayout in particular, already allow you to build complex
 
Search WWH ::




Custom Search