Using GridBagLayout

The GridBagLayout constructor is trivial, GridBagLayout() with no arguments. You use it like this:

GridBagLayout gbl = new GridBagLayout();

Unlike the GridLayout() constructor, this does not say how many rows or columns there will be. This is determined by the cells your program refers to. If you put a component in row 8 and column 2, then Java will make sure there are at least nine rows and three columns. (Rows and columns start counting at zero.) If you later put a component in row 10 and column 4, Java will add the necessary extra rows and columns. You may have a picture in your mind of the finished grid, but Java does not need to know this when you create a GridBagLayout.

Unlike most other LayoutManagers you should not create a GridBagLayout inside a call to setLayout(). You will need access to the GridBagLayout object later in the applet when you add components to the container.


Previous | Next | Top
Last Modified November 11, 1997
Copyright 1997 Elliotte Rusty Harold
elharo@metalab.unc.edu