wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Help needed with GridLayout example 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 "Help needed with GridLayout example" Watch "Help needed with GridLayout example" New topic
Author

Help needed with GridLayout example

Mindy Hudson
Greenhorn

Joined: Dec 08, 2000
Posts: 28
I am experimenting with GridLayout.I thought the following code should give
b1 b2 b3 b4 b5
b6 b7 b8 - -
But when I run it.What I get is
b1 b2 b3 b4
b5 b6 b7 b8.Can some one explain this.What I understood was the rows and cols are allocated first and the components are filled in them.Some one please help me understand.
/**code*/
import java.awt.*;
public class GridAp extends Frame{
public static void main(String argv[]){
GridAp fa=new GridAp();
//Setup GridLayout with 2 rows and 5 columns
fa.setLayout(new GridLayout(2,5));
fa.setSize(400,300);
fa.setVisible(true);
}
GridAp(){
add(new Button("One"));
add(new Button("Two"));
add(new Button("Three"));
add(new Button("Four"));
add(new Button("Five"));
add(new Button("Six"));
add(new Button("Seven"));
add(new Button("Eight"));

}//End of constructor
}//End of Application
Pragya Prakash
Ranch Hand

Joined: Sep 18, 2000
Posts: 42
GridLayout never honors the components´┐Ż preferred size
This layout arranges the components in no of rows/columns specified in the constructor.
But, from my experiments, it seems as if it just honors the no. of rows specified and not the columns, and arranges the components so that they would be equally sized in the matrix.
Regards
Khurram Fakhar
Ranch Hand

Joined: May 29, 2000
Posts: 65
GridLayout , always make division in equal size rectangles,
so if number is Even then it always try to put them in equal size colums , irrespective of rows ..
u can check this by just put the button objects in even number of objcects or Odd ..
Correct me if i m wrong
regards
khurram fakhar
Pragya Prakash
Ranch Hand

Joined: Sep 18, 2000
Posts: 42
i think it honors the no. of rows.
Correct me if I m wrong
Mindy Hudson
Greenhorn

Joined: Dec 08, 2000
Posts: 28
I have no idea about this.I know that when components are more than cells.A column is alloted.But when it is less how is it.Experts please respond.
Cherry Mathew
Ranch Hand

Joined: Dec 26, 2000
Posts: 159
There are two cases in a gridLAyout
first case the number of components added are within the range of number of cells. like in this case 2 rows and 5 columns no. of cells alloted is 10.
and if u add 8 then think it acts like a FlowLayout in arranging it first fills all the columns in the first row and continu to the next row.
but if no of components are more then it keeps no of rows constant and then adds extra columns and fills like as above.

In the code given there are 10 alloted spaces and u r adding 8 components then it first fills 5 columns of first row and goes to next row and puts the three remaining components are put in the second row.
Cherry
natarajan meghanathan
Ranch Hand

Joined: Feb 01, 2001
Posts: 130
I thought it honors the number of rows. But when you replace the constructor with 6 rows and 5 columns instead of 2, it fills two buttons each in a row and gives empty space in the last two rows. How to figure out how it fills?. Its very confusing.
Can any one comment on this?

Sun Certified Programmer for Java 2 Platform
Pragya Prakash
Ranch Hand

Joined: Sep 18, 2000
Posts: 42

Originally posted by Cherry Mathew:

In the code given there are 10 alloted spaces and u r adding 8 components then it first fills 5 columns of first row and goes to next row and puts the three remaining components are put in the second row.
Cherry

Cherry, this is what I expected, i.e.
b1 b2 b3 b4 b5
b6 b7 b8 - -
But when I run it. What I get is
b1 b2 b3 b4
b5 b6 b7 b8.
So, after experimenting with more scenarios, I came to the conclusion, that it honors the no. of rows.
Experts, pls. comment on this.
Thanks,
Pragya
Amit Trivedi
Greenhorn

Joined: Feb 04, 2001
Posts: 25
GridLayout manager behaves strangely when component no is less then no of rows*coulnms
OR no of components > no of rows*coulnms
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi all,
To understand GridLayout you need to look at the code:

Therefore the following:
GridLayout(4,4)
will not divide the screen up into 16 parts! It will divide the screen up into 4 rows. Therefore the following would perform the same:
GridLayout(4,0)
That is why good Java programmers will make sure that the unnecessary parameter is set to zero so that other programmers will know their intent.
Happy programming,
Manfred.
Cherry Mathew
Ranch Hand

Joined: Dec 26, 2000
Posts: 159
Hi,
im very sorry for giving a wrong answer
im sorry that i didnt read the question also
i should have tried the code before answering
Im still not clear why itz acting like this then what is the use of giving the no. of columns. :confused
Cherry
Pragya Prakash
Ranch Hand

Joined: Sep 18, 2000
Posts: 42
Manfred, please explain in more detail.....
What is this code? Is this internal code of GridLayout?
Thanks,
Pragya
Originally posted by Manfred Leonhardt:
[B]Hi all,
To understand GridLayout you need to look at the code:

Therefore the following:
GridLayout(4,4)
will not divide the screen up into 16 parts! It will divide the screen up into 4 rows. Therefore the following would perform the same:
GridLayout(4,0)
That is why good Java programmers will make sure that the unnecessary parameter is set to zero so that other programmers will know their intent.
Happy programming,
Manfred.[/B]

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help needed with GridLayout example
 
Similar Threads
GridLayout
Help needed with emoticons in a chat application
basic things with awt
tictactoe help
Frames