How to use the applet.
The Add button introduces a new component to the layout with a random
color and default GridBagConstraints. The current component is shown ringed by red and green
stripes and it's GridBagConstraints
are those displayed at the top of the applet.
A constraint can be selected by clicking on it and then altered using the
plus and minus buttons or in the case of the insets via a popup dialog.
New values for GridBagConstraints are applied to the component ONLY when the Apply
button is pressed.
The Option button allows the component's text to be changed as well
as it's preferred size if a particular size of component is required.
Components are selected by either shifting the focus or clicking on them. The layout window
is of a fixed size in this applet but in the standalone product it can be
dragged bigger or smaller with the mouse to see what GridBagLayout does with different
container sizes.
Pressing Source will show the resulting source code based on a
notional class with the name SomeComponent. This code can be then used with substitutions
of the actual components. The code generated uses only one instance of GridBagConstraints for
efficiency (GridBagLayout clones the constraints each time they are set).
Remember that weightx or weighty need to have
a value above zero
for the component to expand. These weights are of type double but GridBagger
adjusts them like integer values. This in no way restricts the way the weights can be used.
Instead of 0.4 and 0.6, use the integer values 4 and 6, the effect is the same. Also note that
the insets can be negative and this is meaningful to GridBagLayout, allowing
a component to expand outside it's display area.
This demonstration applet imposes a limit of 5 components in the layout
whereas the commercial product has no limit.
For a detailed explanation of GridBagLayout refer to "The Java Class Libraries, Second Edition
Volume 2"
by Chan & Lee (Addison Wesley).