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’ll call them
“logical” coordinates because they really 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 span more than one row or
column. Components that aren’t as large as their grid cell can
be anchored within their cell. They can also be set to fill or to
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 “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 it is difficult. In short, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access