• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Grid Layout

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic