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 constructor does not specify the number of rows or columns. 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 with most other layout managers, 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 | Cafe au Lait

Copyright 1997, 2006 Elliotte Rusty Harold
Last Modified August 20, 2006