Packages  This Package  Prev  Next  Index  

§1.22 Class GridBagLayout

public  class  java.awt.GridBagLayout
    extends  java.lang.Object  (I-§1.12)
    implements java.awt.LayoutManager  (II-§1.43)
{
        // Fields
    protected final static int MAXGRIDSIZE;	§1.22.1
    protected final static int MINSIZE;	§1.22.2

        // Constructors
    public GridBagLayout();	§1.22.3

        // Methods
    public void addLayoutComponent(String  name, 	§1.22.4
                                          Component  comp);
    public GridBagConstraints getConstraints(Component  comp);	§1.22.5
    public void layoutContainer(Container  target);	§1.22.6
    protected GridBagConstraints	§1.22.7
        lookupConstraints(Component  comp);
    public Dimension minimumLayoutSize(Container  target);	§1.22.8
    public Dimension preferredLayoutSize(Container  target);	§1.22.9
    public void removeLayoutComponent(Component  comp);	§1.22.10
    public void	§1.22.11
        setConstraints(Component  comp, GridBagConstraints  constraints);
    public String toString();	§1.22.12
}
The grid bag layout manager is a flexible layout manager that aligns components horizontally and vertically, without requiring that the components be the same size.

Each grid bag layout manager uses a rectangular grid of cells, with each component occupying one or more cells (called its display area). Each component in a grid bag layout is associated with a set of constraints contained within a GridBag-Constraints instance that specifies how the component is to be laid out within its display area.

The manner in which the grid bag layout manager places a set of components depends on each component's constraints and its minimum size, as well as the preferred size of the components' container.

To use a grid bag layout effectively, one or more components must have a customized GridBagConstraints objects created for it.

The fields of the GridBagConstraints object are described more fully in §1.21.

The following figure shows ten components (all buttons) managed by a grid bag layout manager:

Each of the ten components has the fill field of their constraint set to GridBagConstraints.BOTH. In addition, the buttons have the following non-default constraints:

In addition, the components have the following non-default constraints:

Here is the code that implements the example shown above:


import java.awt.*; import java.util.*; import java.applet.Applet; public class GridBagEx1 extends Applet { protected void makebutton(String name, GridBagLayout gridbag, GridBagConstraints c) { Button button = new Button(name); gridbag.setConstraints(button, c); add(button); } public void init() { GridBagLayout gridbag = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints(); setFont(new Font("Helvetica", Font.PLAIN, 14)); setLayout(gridbag); c.fill = GridBagConstraints.BOTH; c.weightx = 1.0; makebutton("Button1", gridbag, c); makebutton("Button2", gridbag, c); makebutton("Button3", gridbag, c); // end row c.gridwidth = GridBagConstraints.REMAINDER; makebutton("Button4", gridbag, c); c.weightx = 0.0; //reset to the default makebutton("Button5", gridbag, c); //another row // next-to last in row c.gridwidth = GridBagConstraints.RELATIVE; makebutton("Button6", gridbag, c); c.gridwidth = GridBagConstraints.REMAINDER; //end row makebutton("Button7", gridbag, c); c.gridwidth = 1; // reset to the default c.gridheight = 2; c.weighty = 1.0; makebutton("Button8", gridbag, c); c.weighty = 0.0; //reset to the default // end row c.gridwidth = GridBagConstraints.REMAINDER; c.gridheight = 1; // reset to the default makebutton("Button9", gridbag, c); makebutton("Button10", gridbag, c); resize(300, 100); } public static void main(String args[]) { Frame f = new Frame("GridBag Layout Example"); GridBagEx1 ex1 = new GridBagEx1(); ex1.init(); f.add("Center", ex1); f.pack(); f.show(); } }

Fields

MAXGRIDSIZE

protected final static int MAXGRIDSIZE = 128
The maximum number of grid positions (both horizontally and vertically) that can be laid out by the grid bag layout.

MINSIZE

protected final static int MINSIZE = 1
The smallest grid that can be laid out by the grid bag layout.

Constructors

GridBagLayout

public GridBagLayout()
Creates a grid bag layout manager

Methods

addLayoutComponent

public void addLayoutComponent(String name, Component comp)
This method is not used by the grid bag layout manager.
Parameters:
name - a tag understood by the layout manager
comp - the component to be added

getConstraints

public GridBagConstraints getConstraints(Component comp)
Parameters:
comp - the component to be queried
Returns:
the constraint for the specified component in this grid bag layout. A copy of the constraint object is returned.

layoutContainer

public void layoutContainer(Container target)
Lays out the container argument using this grid bag layout.
This method reshapes the components in the specified container in order to satisfy the constraints of this GridBagLayout object.
Most applications do not call this method directly. This method is called when a container calls its layout method (II-§1.11.11).

Parameters:
target - the container in which to do the layout.
See Also:
Container (II-§1.11).

lookupConstraints

protected GridBagConstraints
lookupConstraints(Component comp)
Retrieves the constraints for the specified component in this grid bag layout. The return value is not a copy, but is the actual GridBagConstraints object used by the layout mechanism.
Parameters:
comp - the component to be queried
Returns:
the contraints for the specified component.

minimumLayoutSize

public Dimension minimumLayoutSize(Container target)
Determines the minimum size of the target container using this grid bag layout.
This method is called when a container calls its layout method (II-§1.11.11). Most applications do not call this method directly.

Parameters:
target - the container in which to do the layout
Returns:
the minimum dimensions needed to lay out the subcomponents of the specified container.
See Also:
preferredLayoutSize (II-§1.22.9).

preferredLayoutSize

public Dimension preferredLayoutSize(Container target)
Determines the preferred size of the target container using this grid bag layout.
Most applications do not call this method directly. This method is called when a container calls its preferredSize method (II-§1.11.17).

Parameters:
target - the container in which to do the layout
Returns:
the preferred dimensions to lay out the subcomponents of the specified container.
See Also:
minimumLayoutSize (II-§1.22.8).

removeLayoutComponent

public void removeLayoutComponent(Component comp)
Removes the specified component from this layout.
Most applications do not call this method directly. This method is called when a container calls its remove (II-§1.11.19) or removeAll (II-§1.11.20) methods.

Parameters:
comp - the component to be removed

setConstraints

public void setConstraints(Component comp, GridBagConstraints constraints)
Sets the constraints for the specified component in this layout
Parameters:
comp - the component to be modified
constraints - the constraints to be applied

toString

public String toString()
Returns:
a string representation of this grid bag layout.
Overrides:
toString in class Object (I-§1.12.9).

Packages  This Package  Prev  Next  Index
Java API Document (HTML generated by dkramer on April 22, 1996)
Copyright © 1996 Sun Microsystems, Inc. All rights reserved
Please send any comments or corrections to doug.kramer@sun.com