Click here to Skip to main content
13,055,408 members (53,119 online)
Click here to Skip to main content
Add your own
alternative version


15 bookmarked
Posted 31 Jan 2003

Grid Layouts

, 31 Jan 2003
Rate this:
Please Sign up or sign in to vote.
An introductory article about how to use the 'Grid' Layout in Java.
<!-- Download Links --> <!-- Add the rest of your HTML here -->


This article will demonstrate how to use the 'Grid' Layout in Java. The Grid Layout Managers assigns components to a grid of cells. Each cell in the grid is the same size and the components grow to fill the available area. This Layout Manager is good for laying out containers that look like grids; e.g., a calculator, a calendar page or a battleship game.

The following constructors apply to the Grid Layout:
GridLayout(int rows, int cols, int hgap, int vgap)
GridLayout(int rows, int cols)
The rows is the number of rows in the grid and the cols is the number of columns. At least one of these must be non-zero. A zero means as many rows or columns as necessary. The hgap is the horizontal gap between components and defaults to 0 pixels. The vgap is the vertical gap between components and also defaults to 0 pixels. Example Code:

// Imports
import java.awt.*;
import java.applet.Applet;

public class Grid extends Applet{ 
    // Adding Labels
    Label one   = new Label("Team Name");
    Label two   = new Label("Stadium");
    Label three = new Label("Nick Name");
    Label four  = new Label("Grimsby Town");
    Label five  = new Label("Blundell Park");
    Label six   = new Label("Mariners");
    Label seven = new Label("Kettering Town");
    Label eight = new Label("Rockignham Road");
    Label nine  = new Label("Poppies");
    Label ten   = new Label("Boston United");
    Label eleven= new Label("York Street");
    Label twelve= new Label("Pilgrims");

//The Grid Layout uses the simplest form of the add method which requires only a reference to a component.

    public void init(){

        setLayout(new GridLayout(4,3));
        add(one); one.setBackground(;
        add(two); two.setBackground(;
        add(three); three.setBackground(;
        add(four); four.setBackground(;
        add(five); five.setBackground(;
        add(six); six.setBackground(;
        add(seven); seven.setBackground(;
        add(eight); eight.setBackground(;
        add(nine); nine.setBackground(;
        add(ten); ten.setBackground(;
        add(eleven); eleven.setBackground(;
        add(twelve); twelve.setBackground(;


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Canada Canada
Hi !

You may also be interested in...

Comments and Discussions

GeneralGood Pin
code32829-Aug-08 2:22
membercode32829-Aug-08 2:22 
GeneralNice Article! Pin
CoolPunk1-Feb-03 9:50
memberCoolPunk1-Feb-03 9:50 
GeneralHi Pin
ﻡﺟﻧ_Najm1-Feb-03 9:37
memberﻡﺟﻧ_Najm1-Feb-03 9:37 
GeneralRe: Hi Pin
Achila Liyanarachchi18-Apr-06 21:48
memberAchila Liyanarachchi18-Apr-06 21:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 1 Feb 2003
Article Copyright 2003 by co7eguy
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid