File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Grid Layout Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Grid Layout" Watch "Grid Layout" New topic
Author

Grid Layout

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
this is a question on gridlayout.
There is a gridlayout (2,2). all the four grids are filled up. if i add a new componenent which row will/column it occupy?
Joe
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
If you add a new component the "row" remains same , say 2 in u'r case. Whereas "column" increases depending on how many components you add.

eg:
setLayout(new GridLayout(2,2));
add(new Button("1"));
add(new Button("2"));
add(new Button("3"));
add(new Button("4"));
add(new Button("5"));

The display outputs 2 rows and 3 columns ie., 3 buttons in 1st row and 2 buttons in the 2nd row.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I was wondering about the same thing myself one day so I wrote a little Java program to test it. Just enter the rows and columns on the command line followed by the number of buttons you want.
Hope this helps, Regards, Joe
import java.awt.*;
import java.awt.event.*;
class DynaGrid extends Frame {
DynaGrid(int xval, int yval, int totButtons) {
Panel myPanel = new Panel();
myPanel.setLayout(new GridLayout(xval, yval));

Button bArray[] = new Button[totButtons];
for (int x = 0; x < bArray.length; x++) {
bArray[x] = new Button(" button " + x);
myPanel.add(bArray[x]);
}
add(myPanel);
setSize(600,400);
setVisible(true);
addWindowListener( new WindowAdapter(){
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}

public static void main(String args[]) {
int xval = Integer.parseInt(args[0]);
int yval = Integer.parseInt(args[1]);
int totButtons = 0;
if (args.length == 3)
totButtons = Integer.parseInt(args[2]);
new DynaGrid(xval, yval, totButtons);
}
}
[This message has been edited by Joe Java (edited February 16, 2000).]
[This message has been edited by Joe Java (edited February 16, 2000).]
 
 
subject: Grid Layout