Copyright (c) SEMM NL All rights reserved.
Author : Paul Hamaker. Part of


java.awt.GridBagLayout uses a java.awt.GridBagConstraints object that contains its settings .

The applet's main division is in 2 Panels. The bottom Panel has its layout set to GridBagLayout .

Our method addthosecomponents is called for the first time in the init method with cnt being 1 at that time .

In this method some GridBagConstraints parameters are set .

The case 1 part of the switch is executed, setting the fill parameter as shown. BOTH causes a component to occupy its entire cell.

Leave the switch.

The position parameters for the first component are set .

and this is added to Panel p2 . Note the constraints as second parameter.

The button gets different gridx and gridy settings . The other settings remain as they are.

After adding the components the panel is told to update its visible layout .

When the top button is clicked for the first time, the panel is told to remove all components, so they can be added with different settings later on .

The variable cnt is incremented,....

so the case 2 part of the switch is executed in addthosecomponents and the fill parameter is changed. Components will be as wide as the cells they're in.

With this setting changed, the components will be added again and laid out.

Next click. As high as the cells.

and subsequently . Components at their natural sizes.


GridBagConstraints contains many more settings that need serious experimenting, if you can find the time, doctor.




Default value, other possibilities :


Can't demonstrate cells without these, default values are zero.


Not necessary in this example.