Richard G Baldwin (512) 223-4758,,

JDK 1.1, Controlling Position in a GridLayout

Java Programming, Lecture Notes # 903, Revised 3/20/97.


This is one in a planned series of documents which will be written in response to questions posted on the various Java newsgroups.

In most cases, the response will be in the form of a sample program. In most cases, the programs will be written using JDK 1.1 unless there is some special reason to backtrack to JDK 1.0.2.

Also, in most cases, there will be very little discussion other than the comments contained in the program.

Sample Program

This program was written in response to a request from a poster for code which can be used to control the placement of components in a GridLayout.

/*File Copyright 1997, R.G.Baldwin
This program is designed to be compiled and run under JDK 1.1

This program illustrates use of the GridLayout manager and was written
to answer the following question posted on a newsgroup by another Java 

"I have been looking at your Lecture Notes #118: Placing Components in
Containers - GridLayout.  I have run the examples and examined the code and
seem to understand it fine. I am now trying to write a program that
displays buttons in a panel, not flowed across the panel but specifiy the
coordinates for the buttons.  I do not really want to use absolute
coordinates, because of potential cross platform compatiblity, so I thought
that using GridLayouts will solve this problem.  However, I have a

Once you set the grid spec's and associate it with a panel, is it possible
to say for instance; place button at row 4, column 6 on the grid (not
absolute coordinate).  The Button constructors do not seem to allow for

Any help you can give would be gratefully received.


The answer, is that the positions of individual buttons in a GridLayout 
of buttons is determined by the order that they are added to the container.
However, it is not necessary to be satisfied with that arrangement.  It is
also possible to specify the position of any individual button by using
the two-arg form of the add() method to specify the index of the button
in the list of buttons.

This program creates a keypad emulating those typically found on PC
keyboards.  Initially the keys labeled 7,8,9,4,6,1,2,3,0, and period.
are placed in a GridLayout pattern with a Label object inserted between
the 0-key and the period-key to produce a space in the pattern.  These
keys are positioned by the order in which they are added.  Note that the
5-key is missing in this arrangement.

Then the 5-key is inserted at the proper location using the two-arg
version of the add() method.

A windowClosing() event listener object is instantiated and registered
on the frame to terminate the program when the frame is closed.

The program was tested using JDK 1.1 running under Win95.

This program will be included in the document entitled

"Java903.htm JDK 1.1, Controlling Position in a GridLayout"



import java.awt.*;
import java.awt.event.*;
public class Layout08 {
  public static void main(String[] args){
    GUI gui = new GUI();//instantiate a Graphical User Interface object
  }//end main
}//end class Layout08

class GUI {
  public GUI(){//constructor

    GridLayout myGridLayout = new GridLayout(4,3,8,8);//row,col,Hgap,Vgap

    Frame myFrame = new Frame("Copyright 1997, R.G.Baldwin");

    //Add eleven buttons beginning at the top left.
    myFrame.add(new Button("7 - Home"));
    myFrame.add(new Button("8 - Up"));
    myFrame.add(new Button("9 - PgUp"));
    myFrame.add(new Button("4 - Left"));
    myFrame.add(new Button("6 - Right"));
    myFrame.add(new Button("1 - End"));
    myFrame.add(new Button("2 - Down"));
    myFrame.add(new Button("3 - PgDn"));
    myFrame.add(new Button("0 - Ins"));
    myFrame.add(new Label(""));//insert a blank space    
    myFrame.add(new Button(". - Del"));

    //Insert a button in the center, causing all the others to move
    myFrame.add(new Button("5"), 4);


    //Instantiate and register a window listener to terminate the program
    // when the Frame is closed.    
                          new WindowAdapter(){
                            public void windowClosing(WindowEvent e){
                              System.exit(0);//terminate the program
                            }//end windowClosing()
                          }//end WindowAdapter
                        );//end addWindowListener
  }//end constructor
}//end class GUI definition