This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help needed with GridLayout example

 
Mindy Hudson
Greenhorn
Posts: 28
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think it honors the no. of rows.
Correct me if I m wrong
 
Mindy Hudson
Greenhorn
Posts: 28
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 159
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 130
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Pragya Prakash
Ranch Hand
Posts: 42
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1492
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 159
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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]

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic