• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

GridLayout question

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Class1 extends Frame {
public static void main (String args[]) {
Class1 c1 = new Class1();
c1.setVisible(true);
c1.setSize(100,100);
}
Class1() {
setLayout(new GridLayout(3,2));
add(new Button("1"));
add(new Button("2"));
}
}
How does this layout divides the screen, I though that it will divide the screen in 3 rows and 2 columns and put the "1" and "2" in the first row and first and second column, but what it displayed was screen divide in 3 parts vertically and 2 buttons inthe top 2 parts
Milind
 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I checked out the thing and got the same.
Also, i could make a few observations:
Let us say, you have created a Grid with dimensions 4,3
4 rows, 3 cols.
1. If you add elements <= 4 , then the grid forgets the number of columns add lays out elements one below the other in a single column. For ex, if you have 3 elements,<br /> Element1<br /> Element2<br /> Element3 <br /> 2. If you add elements >4, then you see the second column,but the number of rows is honoured, but the laying out is like
(row1(actually, 0) , col1) , (row1,col2) , (row2,col1) and so on

Element1 Element2
Element3 Element4
Element5 Element6
blankcell blankcell
3.If you add let us say 10 elements , it can't fit in 2 cols (4*2=8) therefore the layout is
Element1 element2 element3
element4 element5 element6
element7 element8 element9
element10 blankcell blankcell
My conclusions are:
1. The number of rows is always honoured
2. More than one column is created only if the number of elements added is more than the number of rows(that answers the original question.) This is inspite of the no. of rows and columns you give.
3. If there is more than one column, the order of layout of the components is always rowwise first.
4. Check out what happens with the following program. This agains ascertains that the number of rows that you give is always honoured, at any cost, but not the number of columns.

import java.awt.*;
public class Class1 extends Frame {
public static void main (String args[]) {
Class1 c1 = new Class1();
c1.setSize(100,100);
c1.setVisible(true);
}
Class1() {
setLayout(new GridLayout(4,3));
add(new Button("1"));
add(new Button("2"));
add(new Button("3"));
add(new Button("4"));
add(new Button("5"));
add(new Button("6"));
add(new Button("7"));
add(new Button("8"));
add(new Button("9"));
add(new Button("10"));
add(new Button("11"));
add(new Button("12"));
add(new Button("13"));
}
}
p.s. If anybody can give any small rule of thumb for this thing, it would be nice. Also, similar things for other layouts.
 
Milind Deodhar
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Vijay, your explanation was very good and I am clear about it now.
 
You ridiculous clown, did you think you could get away with it? This is my favorite tiny ad!
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic