GridBagLayout
The GridBagLayout layout manager is by far the most flexible
layout manager that's included with Java, but it does not enjoy widespread
popularity among Java programmers due to its complexity and its sometimes
non-intuitive behavior.
However, GridBagLayout is often the only layout manager
flexible enough to arrange components in a particular manner, and is used
frequently in spite of the difficulty involved.
As its name implies, GridBagLayout bears some similarity to
GridLayout, but only at a very superficial level. Both divide
the container's available display area into a grid of cells, but beyond
that, GridBagLayout and GridLayout don't have
much in common. Some of the important differences between them include the
following:
- When using a
GridLayout, a component's position within the
grid is determined by the order in which it is added to the container
relative to other components. With a GridBagLayout , you can
explicitly define the component's location within the grid.
- Each component in a
GridLayout occupies exactly one cell
in the grid, but components managed by a GridBagLayout can
span multiple rows and/or columns within the grid.
GridLayout assigns each row the same height and each
column the same width, which causes every cell in the grid to have exactly
the same dimensions. In contrast, GridBagLayout allows each
row to have a separate height and every column its own width, so every cell
in the grid can theoretically have a unique size.
GridLayout does not support any constraints, while
GridBagLayout allows you to specify a different set of
constraint values for each component, and those constraints allow you to
customize the component's size and position within the grid.
If you're not already familiar with it, you may be wondering why
GridBagLayout is considered so difficult to use by many
Java programmers. Some of the possible reasons are:
- The number of constraints and their interactions
GridBagConstraints encapsulates eleven different constraint
values, and each child component is assigned its own instance of
GridBagConstraints. Although no single constraint is
particularly difficult to understand, the way in which the constraints
interact with one another and with the constraints of other components is
somewhat complex.
- Row height and column widths
GridBagLayout 's ability to provide a separate height for each row and width for each
column is one of its primary advantages, but that capability also adds a great deal of
complexity to its use. In some cases, especially with complex layouts containing many
components, it can be difficult to predict what a component's size or position will be, and it's
easy to make mistakes that produce results different from what you expected.
- Component location
When you see a component inside a GridLayout, it's usually easy to identify which cell the
component occupies without examining the source code. That's because all cells (and
components) are the same size, and the cells are aligned with one another. In the case of a
GridBagLayout, identifying which cell or cells a component occupies can be difficult, since
cell widths and heights can vary and since a component can span multiple cells.
- Component size
Most other layout managers have simple rules that determine the size that a component is set
to, but GridBagLayout provides much greater flexibility in this area, as well as more complexity.
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|