All About Class GridLayout in AWT in Java

GridLayout in AWT in Java
GridLayout in AWT in Java

GridLayout in AWT in Java:

GridLayout provides flexibility for placing components by implementing the LayoutManager interface to lay out 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 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.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 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 void int getColumns();
public void setColumns(int cols);
public void int getHgap();
public void setHgap(int hgap);
public void int getRows();
public void setRows(int rows);
public void int getVgap();
public void setVgap(int vgap);
public String toString();
}
 
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.
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 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 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.

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.
All About Class GridLayout in AWT in Java All About Class GridLayout in AWT in Java Reviewed by Animesh Chatterjee on October 23, 2018 Rating: 5

No comments:

Powered by Blogger.