Class GridLayout in AWT in Java

0
201
Spread the love
  • 24
  • 34
  • 28
  • 35
  •  
  •  
  •  
  • 12
  •  
  •  
    133
    Shares

GridLayout in AWT in Java:

GridLayout provides flexibility for placing components by implementing the LayoutManager interface to layout component objects in a container. It allows us to build a table of components and as we add them, they are placed left to right and top to bottom in the grid. So, It causes the container’s component to be laid out in a rectangular grid.

The container is split into equal-sized rectangles. If either the number of rows or the number of columns is set to zero, its value is computed from other dimensions and the total number of components. In GridLayout the components are resized to fill the row-column area. If it allows, GridLayout can reposition or resize objects after adding or removing components. Whenever the area is resized, the components within the area are also resized.

One component is placed into each rectangle. We can create the manager with a number of rows and columns. Components then fill up the calls defined by the manager. In the constructor, we need to specify the number of rows and columns that we need and these are laid out in equal proportion.

The GridLayout LayoutManager provides the means to layout components in a grid of rows and columns.

The class structure of class GridLayout is given below:

public class java.awt.GridLayout extends java.lang.Object implements java.awt.LayoutManager,                                 java.io.Serializable{
//constructor
public GridLayout();
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 removeLayoutSize(Container target);
public void removeLayoutComponent(Component comp);
public int getColumns();
public void setColumns(int cols);
public int getHgap();
public void setHgap(int hgap);
public int getRows();
public void setRows(int rows);
public int getVgap();
public void setVgap(int vgap);
public String toString();
} 

The details of the class structure are given as follows:

public GridLayout();

public GridLayout() constructs a GridLayout object with a default single row and one column per component.

public GridLayout (int rows, int cols);

public GridLayout (int rows, int cols) constructs a GridLayout object with the requested number of rows and columns. Note that the actual number of rows and columns depends on the number of objects in the layout, not the constructor’s parameters.

Parameter
rows – Requested number of rows in the container.
cols – Requested number of columns in container.

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

public GridLayout (int rows, int cols, int hgap, int vgap) constructs a GridLayout object with the requested number of rows and columns and the values specified as the gaps between each component. Note that the actual number of rows and columns depends on the number of objects in the layout, not the constructor’s parameters.

Parameter
rows – Requested number of rows in the container.
cols – Requested number of columns in the container.
hgap – Horizontal space between each component in a row.
vgap – Vertical space between each row.

public void addLayoutComponent (String name, Component component);

public void addLayoutComponent (String name, Component component) method actually does nothing.

Parameter
name – Name of the component to add.
component – Actual component being added.

public int getColumns();

public int getColumns() method returns the number of columns.

This method returns the number of columns.

public int getHgap();

public int getHgap() method returns the horizontal gap for this GridLayout instance.

This method returns the horizontal gap for this GridLayout instance.

public int getRows();

public int getRows() method returns the number of rows.

This method returns the number of rows.

public int getVgap();

public int getVgap() method returns the vertical gap for this GridLayout instance.

This method returns the vertical gap for this GridLayout instance.

public void layoutContainer (Container target);

public void layoutContainer (Container target) method draws the components contained within the target.

Parameter
target – The container that needs to be redrawn.

public Dimension minimumLayoutSize (Container target);

public Dimension minimumLayoutSize (Container target) method Calculates the minimum size of the target container.

Parameter
target – The container whose size needs to be calculated.

public Dimension preferredLayoutSize (Container target);

public Dimension preferredLayoutSize (Container target) method calculates the preferred size of the target container.

This method returns the Preferred Dimension of the container target.
Parameter
target – The container whose size needs to be calculated.

public void removeLayoutComponent (Component component);

public void removeLayoutComponent (Component component) method actually does nothing.

Parameter
component – Component to stop tracking.

public void setColumns(int cols);

public void setColumns(int cols) method sets the number of columns

Parameter
cols – The new number of columns.

public void setHgap(int hgap);

public void setHgap(int hgap) method sets the horizontal gap between components.

Parameter
hgap – The horizontal gap value.

public void setRows(int rows);

public void setRows(int rows) method sets the number of rows.

Parameter
rows – The new number of rows.

public void setVgap(int vgap);

public void setVgap(int vgap) method sets the vertical gap between components.

Parameter
vgap – The vertical gap value.

public String toString();

public String toString() method returns a string representation of the GridLayout object.

This method returns a string representation of the GridLayout object.

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 an equal size. One but not both rows and columns can be zero, which means that any number of objects can be placed in a row or in a column.
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 an equal size. In addition, the horizontal and vertical 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 row. One but not both rows and columns can be zero, which means that any number of objects can be placed in a row or in a column.
Example


Button b1,b2,b3,b4,b5,b6;
Frame frame=new Frame();
frame.setLayout(new GridLayout(3,2));//3 rows and 2 columns
b1=new Button("B1");
b2=new Button("B2");
b3=new Button("B3");
b4=new Button("B4");
b5=new Button("B5");
b6=new Button("B6");
frame.add(b1);frame.add(b2);frame.add(b3);
frame.add(b4);frame.add(b5);frame.add(b6);
 

In case if the last slot is empty, no balancing goes on with GridLayout.

Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 24
  • 34
  • 28
  • 35
  •  
  •  
  •  
  • 12
  •  
  •  
    133
    Shares

LEAVE A REPLY

Please enter your comment!
Please enter your name here