Setting the Location of a Component in a GridBagLayout
A gridbag layout arranges components in a two-dimensional grid of
cells. The northwest-most cell has position (0,0). The cell to
the right or east has position (1,0); the cell to the bottom or south has position
(0,1).
The size of the grid grows dynamically.
For example, if you place a component at position (2,1)
in a new empty gridbag layout, the layout will
automatically have a size of 3 cells across by 2 cells down.
See Creating a GridBagLayout for an example on how to
use a gridbag layout with gridbag constraints.
GridBagConstraints gbc = new GridBagConstraints();
// Place a component at (1,1)
gbc.gridx = 1;
gbc.gridy = 1;
gbl.setConstraints(component1, gbc);
container.add(component1);
// The layout now has 4 cells
// Place a component at (0,0)
gbc.gridx = 0;
gbc.gridy = 0;
gbl.setConstraints(component2, gbc);
container.add(component2);
Post a comment