CS535 Object-Oriented Programming & Design
Fall Semester, 1996
Doc 28, AWT Gridbag
[To Lecture Notes Index]
San Diego State University -- This page last updated Nov 25, 1996

Contents of Doc 28, AWT Gridbag
- References
Core Java, Chapter 7
Graphic Java, Geary and McClellan, Chapter 7
GridBagConstrants
gridx, gridy
- Column and row positions of the upper left hand corner of the component to
be added
gridwidth, gridheight
- Number of columns and rows the component will occupy
- REMAINDER
- RELATIVE
fill
- Determines if component fills the entire area
- HORIZONTAL
- VERTICAL
- NONE
- BOTH
anchor
- If the component does not fill the entire space, anchor indicates where if
will be placed
| CENTER | NORTHEAST | SOUTHEAST |
| EAST | NORTHWEST | SOUTHWEST |
| NORTH | SOUTH | WEST |
insets
- Margin around the outside of a component
ipadx, ipady
- Added to the minimum size of the component
weightx, weighty
- How much of available space should the component take
Fill Example
class GridBagExample extends Frame {
public GridBagExample( int width, int height ) {
setTitle( "Grid Bag" );
resize( width, height );
GridBagLayout aLayout = new GridBagLayout();
GridBagConstraints constraints = new GridBagConstraints();
setLayout( aLayout );
constraints.gridx = 0; // Start column
constraints.gridy = 0; // Start row
constraints.gridwidth = 2; // Num. of columns wide
constraints.gridheight = 1; // Num. of rows high
constraints.weighty = 100;
constraints.weightx = 100;
constraints.anchor = GridBagConstraints.CENTER;
constraints.fill = GridBagConstraints.NONE;
Button none = new Button( "none" );
aLayout.setConstraints( none, constraints );
constraints.gridy = 1;
constraints.fill = GridBagConstraints.HORIZONTAL;
Button horz = new Button( "horz" );
aLayout.setConstraints( horz, constraints );
constraints.gridy = 2;
constraints.fill = GridBagConstraints.BOTH;
Button both = new Button( "both" );
aLayout.setConstraints( both, constraints );
constraints.gridy = 3;
constraints.fill = GridBagConstraints.VERTICAL;
Button vert = new Button( "vert" );
aLayout.setConstraints( vert, constraints );
add( none ); add( both );
add( horz ); add( vert );
show();
}
}
Fill Example
Anchor Example
class GridBagExample extends Frame {
public GridBagExample( int width, int height ) {
setTitle( "Grid Bag" );
resize( width, height );
GridBagLayout aLayout = new GridBagLayout();
GridBagConstraints constraints = new GridBagConstraints();
setLayout( aLayout );
constraints.gridx = 0; // Start column
constraints.gridy = 0; // Start row
constraints.gridwidth = 1; // Num. of columns wide
constraints.gridheight = 1; // Num. rows high
constraints.weighty = 100;
constraints.weightx = 100;
constraints.anchor = GridBagConstraints.CENTER;
constraints.fill = GridBagConstraints.NONE;
Button center = new Button( "center" );
aLayout.setConstraints( center, constraints );
constraints.gridx = 1;
constraints.anchor = GridBagConstraints.WEST;
Button west = new Button( "west" );
aLayout.setConstraints( west, constraints );
constraints.gridx = 2;
constraints.anchor = GridBagConstraints.NORTHWEST;
Button nwest = new Button( "nwest" );
aLayout.setConstraints( nwest, constraints );
constraints.gridx = 3;
constraints.anchor = GridBagConstraints.NORTHEAST;
Button neast = new Button( "neast" );
aLayout.setConstraints( neast, constraints );
add( center ); add( nwest );
add( west ); add( neast );
show();
}
}
Anchor Example
Inset Example
class GridBagExample extends Frame {
public GridBagExample( int width, int height ) {
setTitle( "Insets" );
resize( width, height );
GridBagLayout aLayout = new GridBagLayout();
GridBagConstraints constraints = new GridBagConstraints();
setLayout( aLayout );
constraints.gridwidth = 1; // Num. of columns wide
constraints.gridheight = 1; // Num. rows high
int top = 10; int bottom = 20;
int left = 12; int right = 0;
constraints.insets = new Insets( top, left , bottom, right );
constraints.fill = GridBagConstraints.BOTH;
constraints.weighty = 100;
constraints.weightx = 100;
constraints.gridx = 2;
constraints.gridy = 0;
Button one = new Button( "one" );
aLayout.setConstraints( one, constraints );
constraints.gridx = 1;
Button two = new Button( "two" );
aLayout.setConstraints( two, constraints );
constraints.gridx = 0;
Button three = new Button( "three" );
aLayout.setConstraints( three, constraints );
add( one ); add( two );
add( three );
show();
}
}
Insets Example
int top = 10; int bottom = 20;
int left = 12; int right = 0;
constraints.insets = new Insets( top, left , bottom, right );
Relative/Remainder Example
Button1, Button2, Button3: weightx=1.0
Button4: weightx=1.0, gridwidth=GridBagConstraints.REMAINDER
Button5: gridwidth=GridBagConstraints.REMAINDER
Button6: gridwidth=GridBagConstraints.RELATIVE
Button7: gridwidth=GridBagConstraints.REMAINDER
Button8: gridheight=2, weighty=1.0,
Button9, Button 10: gridwidth=GridBagConstraints.REMAINDER