A GridBagConstraints object specifies the location and area of the component's display area within the container and how the component is laid out inside its display area. The GridBagConstraints, in conjunction with the component's minimum size and the preferred size of the component's container, determines where the component is placed within the applet.

The GridBagConstraints() constructor is trivial

GridBagConstraints gbc = new GridBagConstraints();

Your interaction with a GridBagConstraints object takes place through its eleven fields and fifteen mnemonic constants.

I'll demonstrate by showing you how to lay out this calculator. Notice that it has a TextField and 18 buttons of varying sizes, and naturally lends itself to a description as a grid.

Calculator Applet

The discussion assumes that you have imported all the necessary classes, particularly java.awt.GridBagLayout, java.awt.GridBagConstraints, java.awt.Button and java.awt.TextField. It also assumes you have created a GridBagConstraints object named displayConstraints like this

GridBagConstraints displayConstraints = new GridBagConstraints();

Previous | Next | Top | Cafe au Lait

Copyright 1997-1999 Elliotte Rusty Harold
Last Modified June 30, 1999