• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

GridLayout

 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hai Ranchers,
Ref
GridLayout program below &
RHE Page No 297 Quote" Grid layout managers behave starngely when u have them manage very few or very many components (that is more less than or than the number of rows and coloumns);"

Here
No of cells 8; Components added 12;
Guess the o/p.
Comeback; I have more to discuss
------------------------------------------------------


import java.awt.*;
import java.applet.*;
public class Lay4 extends Applet
{
Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12;
public void init()
{
setLayout(new GridLayout(2,4));
b1=new Button("Button 1");
add(b1);
b2=new Button("Button 2");
add(b2);
b3=new Button("Button 3");
add(b3);
b4=new Button("Button 4");
add(b4);
b5=new Button("Button 5");
add(b5);
b6=new Button("Button 6");
add(b6);
b7=new Button("Button 7");
add(b7);
b8=new Button("Button 8");
add(b8);
b9=new Button("Button 9");
add(b9);
b10=new Button("Button 10");
add(b10);
b11=new Button("Button 11");
add(b11);
b12=new Button("Button 12");
add(b12);

}
}

[This message has been edited by tvs sundaram (edited August 15, 2001).]
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
tvs
When you write "new GridLayout(3,4)", the compiler really only looks at the number of rows and calculates the number of columns. So it is better to set the number of columns to 0.
If you want to set the number of columns and let the compiler work out the number of rows, use "new GridLayout(0,4)"

There is an excellent tutorial on Layout at

http://developer.java.sun.com/developer/onlineTraining/GUI/AWTLayoutMgr/shortcourse.html
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi TVS,

The output for your code is 2 rows 6 cols.
The no of rows and col are decided by the following logic in GridLayout
if (nrows > 0) {
ncols = (ncomponents + nrows - 1) / nrows;
else
nrows = (ncomponents + ncols - 1) / ncols;

for eg
1. f.setLayout(new GridLayout(3,4)); here no of rows are taken as a base and col are calculated with recpect to the no of components.
2. f.setLayout(new GridLayout(3,0)); same as step 1.
3. f.setLayout(new GridLayout(0,4)); here no of cols are taken as a base and rows are calculated with recpect to the no of components.

4. f.setLayout(new GridLayout(0,0)); This is illegal

As per the logic if I add only one component in your code that
component is occupying the full layout instead of creating 2 rows and one colm. Can anybody help .

Thanks


[This message has been edited by INDU, BALA (edited August 15, 2001).]
 
tvs sundaram
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hai Indu,
Your Quote
-----------------------------
The no of rows and col are decided by the following logic in GridLayout
if (nrows > 0) {
ncols = (ncomponents + nrows - 1) / nrows;
else
nrows = (ncomponents + ncols - 1) / ncols;
-----------------------------
From where u got this logic?
However, I am yet to check it for all conditions..
tvs sundaram.

 
INDU, BALA
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi TVS,

Murphy already posted this url.Just take a look here
http://developer.java.sun.com/developer/onlineTraining/GUI/AWTLayoutMgr/sho rtcourse.html#gridLayout
Hope this helps
 
tvs sundaram
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Friends,
Thank you for your response.
It helps.
tvs sndaram
 
please buy this thing and then I get a fat cut of the action:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic