5,662,937 members and growing! (117 online)
Email Password   helpLost your password?
Desktop Development » Grid & Data Controls » Grid controls     Beginner

Grid Layouts

By co7eguy

An introductory article about how to use the 'Grid' Layout in Java.
Java, Java, Dev

Posted: 31 Jan 2003
Updated: 31 Jan 2003
Views: 44,785
Bookmarked: 7 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
16 votes for this Article.
Popularity: 2.54 Rating: 2.11 out of 5
9 votes, 56.3%
1
1 vote, 6.3%
2
1 vote, 6.3%
3
0 votes, 0.0%
4
5 votes, 31.3%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Introduction

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(Color.red);
        add(two); two.setBackground(Color.red);
        add(three); three.setBackground(Color.red);
        add(four); four.setBackground(Color.green);
        add(five); five.setBackground(Color.green);
        add(six); six.setBackground(Color.green);
        add(seven); seven.setBackground(Color.blue);
        add(eight); eight.setBackground(Color.blue);
        add(nine); nine.setBackground(Color.blue);
        add(ten); ten.setBackground(Color.orange);
        add(eleven); eleven.setBackground(Color.orange);
        add(twelve); twelve.setBackground(Color.orange);
    }
}

License

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

co7eguy


Hi !
Location: Canada Canada

Other popular Grid & Data Controls articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 4 of 4 (Total in Forum: 4) (Refresh)FirstPrevNext
GeneralGoodmembercode3283:22 29 Aug '08  
GeneralNice Article!memberCoolPunk10:50 1 Feb '03  
GeneralHimemberﻡﺟﻧ_Najm10:37 1 Feb '03  
GeneralRe: HimemberAchila Liyanarachchi22:48 18 Apr '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 31 Jan 2003
Editor:
Copyright 2003 by co7eguy
Everything else Copyright © CodeProject, 1999-2008
Java | Advertise on the Code Project