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

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "About AWT!" Watch "About AWT!" New topic

About AWT!

Ranch Hand

Joined: Sep 14, 2000
Posts: 30
what happens if you add more buttons to GridLayout than can fit and fully display the button labels?
1)the size of the container is increased to allow the button labels to fully display.
2)the GridLayout ignores the size of the label and the labels will be truncated.
3)a compile time error indicating the buttons cannot to the preferred size.
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I guess the answer is 2 coz mu understanding of GridLayout is that they ignore the preferred size of a components. Therefore, I guess 2 should be the right answer.
Somebody, please confirm this
Ira Jain
Ranch Hand

Joined: Sep 06, 2000
Posts: 70
sampaths77 ,you are right.The answer will be second because GridLayout always ignores a component's preferred size.
Sagar Sharma
Ranch Hand

Joined: Aug 31, 2000
Posts: 92
if you have 4 cells ( 2 rows and 2 columns) and all cells are filled by 1 button each. Now if you try to add another button a column is added to the grid and the newly added button goes in row=2 amd column =2
the final arrangement is... (1,1) (1,2) (1,3) (2,1) (2,2)
accordingly (a) is the answer...please do check and find out if this is true.
mail me back regarding the same at
Ranch Hand

Joined: Sep 14, 2000
Posts: 30
2 is the right answer. But I have done the test. it shows the right answer 1 is right. so I got comfused.
Ranch Hand

Joined: Sep 10, 2000
Posts: 31
hi all:
I think the only correct answer is 2.just try below code to validate it.
import java.awt.*;
public class test extends Frame{
public static void main(String agrs[]){
test t = new test();
super("my frame");
setLayout(new GridLayout(2,2));
add(new Button("botton1"));
add(new Button("botton1"));
add(new Button("botton1"));
add(new Button("botton1"));
add(new Button("botton1"));
you will find the Frame's size isn't increased to allow the buttons full showed.In the contrast,the Button will be truncated.
Ranch Hand

Joined: Sep 12, 2000
Posts: 63
The buttons will be displayed accurately if we add a pack() method for the frame before the setVisible() method. But without the pack() method, I think 2 is the right answer.
Also, the API documentation says the following for GridLayout:
"When both the number of rows and the number of columns have been set to non-zero values, either by a constructor or by the setRows and setColumns methods, the number of columns specified is ignored. Instead, the number of columns is determined from the specified number or rows and the total number of components in the layout. So, for example, if three rows and two columns have been specified and nine components are added to the layout, then they will be displayed as three rows of three columns. Specifying the number of columns affects the layout only when the number of rows is set to zero."
Hope this helps.
I agree. Here's the link:
subject: About AWT!
It's not a secret anymore!