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

Class GridLayout

Constructors , Methods

public  class  java.awt.GridLayout
    extends  java.lang.Object  
    implements java.awt.LayoutManager  
{
        // Constructors
    public GridLayout(int  rows, int  cols);	
    public GridLayout(int  rows, int  cols,	
                       int  hgap, int  vgap);
        // Methods
    public void addLayoutComponent(String  name,	
                              Component  comp);
    public void layoutContainer(Container  target);	
    public Dimension minimumLayoutSize(Container  target);	
    public Dimension preferredLayoutSize(Container  target);	
    public void removeLayoutComponent(Component  comp);	
    public String toString();	
}

This grid layout manager causes the container's components to be laid out in a rectangular grid. The container is split into equal-sized rectangles: one component is placed into each rectangle.

For example, the following code says to lay out the six buttons into three rows and two columns:


import java.awt.*;
import java.applet.Applet;
public class buttonGrid extends Applet {
  public void init() {
    setLayout(new GridLayout(3,2));
        add(new Button("1"));
    add(new Button("2"));
    add(new Button("3"));
    add(new Button("4"));
    add(new Button("5"));
    add(new Button("6"));
  }
}


It produces the following output:


Constructors


GridLayout

public GridLayout(int  rows, int  cols) 

Creates a grid layout with the specified number of rows and columns. All components in the layout are given equal size.

One, but not both of rows and columns can be zero, which means that any number of objects can be placed in a row or in a column

ParameterDescription
rows the rows; zero means "any number"
cols the columns; zero means "any number"


GridLayout

public GridLayout(int  rows, int  cols, int  hgap, int  vgap) 

Creates a grid layout with the specified number of rows and columns. All components in the layout are given equal size.

In addition the horizontal and vertical gaps are set to the specified values. The horizontal gaps are placed at the left and right edge, and between each of the columns. The vertical gaps are placed at the top and bottom edge, and between each of the rows.

One, but not both of rows and columns can be zero, which means that any number of objects can be placed in a row or in a column

ParameterDescription
rows the rows; zero means "any number"
cols the columns; zero means "any number"
hgap the horizontal gap
vgap the vertical gap


Methods


addLayoutComponent

public void addLayoutComponent(String  name, Component  comp) 

This method is not used by the grid layout manager.

ParameterDescription
name a tag
comp the component to be added


layoutContainer

public void layoutContainer(Container  target) 

Lays out the container argument using this layout.

This method reshapes the components in the specified target container in order to satisfy the constraints of the GridLayout object.

The grid layout manager determines the size of individual components by dividing the free space in the container into equal-sized portions according to the number of rows and columns in the layout. The container's free space equals the container's size minus any insets and any specified horizontal or vertical gap. All components in a grid layout are given the same size

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 .


minimumLayoutSize

public Dimension minimumLayoutSize(Container  target) 

Determines the minimum size of the container argument using this grid layout.

The minimum width of a grid layout is the largest minimum width of any of the widths in the container times the number of columns, plus the horizontal padding times the number of columns plus 1, plus the left and right insets of the target container.

The minimum height of a grid layout is the largest minimum height of any of the widths in the container times the number of rows, plus the vertical padding times the number of rows plus 1, plus the top and left insets of the target container.

Most applications do not call this method directly. This method is called when a container calls its layout method .

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 container argument using this grid layout.

The preferred width of a grid layout is the largest preferred width of any of the widths in the container times the number of columns, plus the horizontal padding times the number of columns plus 1, plus the left and right insets of the target container.

The preferred height of a grid layout is the largest preferred height of any of the widths in the container times the number of rows, plus the vertical padding times the number of rows plus 1, plus the top and left insets of the target container.

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) 

This method is not used by the grid layout manager.

ParameterDescription
comp the component to be removed


toString

public String toString() 

Return Value:

Returns a string representation of this grid layout.

Overrides:

toString in class Object .



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