Home Segments Top Top Previous Next

1104: Mainline

Once you have set the weightx, weighty, and fill instance variables, you can place individual components.

First, you condition the placement of the next component by making adjustments to gridx and gridy instance variables of the GridBagConstraints instance:

gbc.gridx = 1; <-- Upper-left corner of component is in the second row 
gbc.gridy = 2; <-- and third column 

Second, you specify the number of rows and columns that the component is to span by adjusting gridwidth and gridheight instance variables:

gbc.gridwidth = 2;  <-- Component spans two columns 
gbc.gridheight = 1; <-- and one row 

Third, you associate the constraint with a component using setConstraints, an instance method of the GridBagLayout class:

 *-- A gridbag layout 
 | 
 |                 *-- A component 
 |                 | 
 |                 |   *-- A gridbag constraint 
 v                 v   v 
gbl.setConstraints(p, gbc); 

Fourth, and finally, you add the component to the layout using add:

add(p);