Package java.awt Previous
Previous
Java API
Java API
Index
Index
Next
Next

Class GridBagLayout

Fields , Constructors , Methods

public  class  java.awt.GridBagLayout
    extends  java.lang.Object  
    implements java.awt.LayoutManager  
{
        // Fields
    protected final static int MAXGRIDSIZE;	
    protected final static int MINSIZE;	

        // Constructors
    public GridBagLayout();	

        // Methods
    public void addLayoutComponent(String  name, 	
                                          Component  comp);
    public GridBagConstraints getConstraints(Component  comp);	
    public void layoutContainer(Container  target);	
    protected GridBagConstraints	
        lookupConstraints(Component  comp);
    public Dimension minimumLayoutSize(Container  target);	
    public Dimension preferredLayoutSize(Container  target);	
    public void removeLayoutComponent(Component  comp);	
    public void	
        setConstraints(Component  comp, GridBagConstraints  constraints);
    public String toString();	
}

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 Class GridBagConstraints.

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.

ParameterDescription
name a tag understood by the layout manager
comp the component to be added


getConstraints

public GridBagConstraints getConstraints(Component  comp) 

Return Value:

Returns the constraint for the specified component in this grid bag layout. A copy of the constraint object is returned.

ParameterDescription
comp the component to be queried


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 .

ParameterDescription
target the container in which to do the layout.

See Also: Container .


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.

Return Value:

Returns the contraints for the specified component.

ParameterDescription
comp the component to be queried


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 . Most applications do not call this method directly.

Return Value:

Returns the minimum dimensions needed to lay out the subcomponents of the specified container.

ParameterDescription
target the container in which to do the layout

See Also: preferredLayoutSize .


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 .

Return Value:

Returns the preferred dimensions to lay out the subcomponents of the specified container.

ParameterDescription
target the container in which to do the layout

See Also: minimumLayoutSize .


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 or removeAll methods.

ParameterDescription
comp the component to be removed


setConstraints

public void setConstraints(Component  comp, 
                                                     GridBagConstraints  constraints) 

Sets the constraints for the specified component in this layout

ParameterDescription
comp the component to be modified
constraints the constraints to be applied


toString

public String toString() 

Return Value:

Returns a string representation of this grid bag layout.

Overrides:

toString in class Object .



Top© 1996 Sun Microsystems, Inc. All rights reserved.