File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes GridLayout Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "GridLayout" Watch "GridLayout" New topic
Author

GridLayout

tvs sundaram
Ranch Hand

Joined: Jan 28, 2001
Posts: 153
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).]
Siobhan Murphy
Ranch Hand

Joined: Oct 19, 2000
Posts: 72
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
INDU, BALA
Ranch Hand

Joined: Jun 07, 2001
Posts: 48
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

Joined: Jan 28, 2001
Posts: 153
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

Joined: Jun 07, 2001
Posts: 48
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

Joined: Jan 28, 2001
Posts: 153
Friends,
Thank you for your response.
It helps.
tvs sndaram
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: GridLayout
 
Similar Threads
Help needed with emoticons in a chat application
javax.crypto.BadPaddingException for AES when encrypting and decrypting multiple times
tictactoe help
problem with handling the states of the events
Please need help with calculator program...