GridBagLayout
GridBagLayout is a very flexible
layout manager that allows you to position components relative to one
another using constraints. With GridBagLayout (and a fair amount of effort), you
can create almost any imaginable layout. Components are arranged at
logical coordinates on an abstract grid. We call them “logical”
coordinates because they designate positions in the space of rows and
columns formed by the set of components. Rows and columns of the grid
stretch to different sizes, based on the sizes and constraints of the
components they hold.
A row or column in a GridBagLayout expands to accommodate the
dimensions and constraints of the largest component it contains.
Individual components may also be told to span more than one row or
column. Components that aren’t as large as their grid cell can be anchored
(positioned to one side) within their cell. They can also be set to fill
or expand their size in either dimension. Extra area in the grid rows and
columns can be parceled out according to the weight constraints of the
components. In this way, you can control how various components will grow
and stretch when a window is resized.
GridBagLayout is much easier to
use in a graphical WYSIWYG GUI builder environment. That’s because working
with GridBag is kind of like messing with the old rabbit-ears antennae on your television. It’s not particularly difficult to get the results that you want through trial and error, but writing out hard and fast rules for how to go about ...