Wednesday, July 31, 2013


GridLayout is another layout manager. It displays components in a grid or table. A cell of the grid or table is for one component. You have three ways to create a grid layout manager by using the following constructors:

creates a grid layout that has only one column.
-GridLayout(int rows, int columns)
creates a grid layout with specified number of rows and columns.
-GridLayout(int rows,int columns,int hor,int ver)
creates a grid layout with specified number of rows and columns, and spaces between components.

In the example code below, a grid layout with five rows and two columns is created. Each cell of the grid contains a button object.

import java.awt.Button;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

class InterfaceShow extends Frame{
InterfaceShow(String title){
setLayout(new GridLayout(5,2));
setSize(new Dimension(500,300));

addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
for(int i=0;i<10;i++){

add(new Button(i+""));

public class GridLayoutTest {
public static void main(String[] args){
new InterfaceShow("Using GridLayout");


gridlayout layoutmanager


  1. When i use GridLayout to arrange component in a table form, all components are resized to fit the JFrame window. Actually, i don't want to the components to be resized in this way. Is there any solution?

  2. The solution is simple. Firstly, set the layout of the frame window to FlowLayout(setLayout(new FlowLayout()). Then create a panel container. You will need to set the layout of the panel to GridLayout. All your components will placed in the panel. Finally, add the panel to the frame window by using its add(Component c) method.