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 GridBagLayout problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "GridBagLayout problem" Watch "GridBagLayout problem" New topic

GridBagLayout problem

Ranch Hand

Joined: Jan 15, 2001
Posts: 162
In the following question from JXAM, as per author options 1,4 are correct.
I think Option 1, should not be correct for infact it will result in one row and four columns.

Consider the following piece of code, and select the correct statement(s):
public class Test extends Applet{
public void init(){
setLayout(new GridLayout(1,2));
add(new Button("#1"));
add(new Button("#2"));
add(new Button("#3"));
add(new Button("#4"));

The Gridlayout is created with 1 row and 2 columns.
Adding the button with the label "#3" will cause an exception to be thrown.
Adding the button with label "#3" will cause it to overwrite the button with label "#2".
The layout automatically extends to accommodate the additional buttons.
Only 2 buttons are displayed, one with label "#1" and one with label "#4".
Mikael Jonasson
Ranch Hand

Joined: May 16, 2001
Posts: 158
As far as I can see 1 & 4 is correct. The grid is initially created with 1 row and 2 columns. However adding more components to a grid then fits, will add new rows, so when the code you've written is done, the grid will consist of 2 rows with 2 columns.
Or to do step by step:
1: Correct, thats what the "setLayout(new GridLayout(1,2));"-line does.
2: Wrong. An extra row is added automaticly to the grid.
3: Wrong. See 2.
4: Correct. See 2.
5: See 2.
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Originally posted by Mikael Jonasson:
However adding more components to a grid then fits, will add new rows, so when the code you've written is done, the grid will consist of 2 rows with 2 columns.

If you specify both a row and a column value greater than zero then the the number of columns value is ignored, and the number of columns is determined based on the number of rows and the number of components added. In the example Zeeshan gave it will create 1 row with 4 columns. If you create a Frame with 2 rows and 4 columns each but then add 10 components you'll get a grid of 2 rows with 5 columns each.

I think number one is correct because the layout is initially created with one row and 2 columns, it is later modified or extended to be one row with 4 columns. So I guess it's the way it is worded.

hope that helps

Paul Michael
Ranch Hand

Joined: Jul 02, 2001
Posts: 697
hi ZEESHAN! i think your title is a bit misleading. you wrote GridBagLayout problem in your topic but your actual question is about GridLayout. it's not much of a problem though but it could actually save those people searching for GridBagLayout related posts time.

SCJP 1.2 (89%), SCWCD 1.3 (94%), IBM 486 (90%), SCJA Beta (96%), SCEA (91% / 77%), SCEA 5 P1 (77%), SCBCD 5 (85%)
Ji Wang

Joined: Feb 05, 2001
Posts: 13

The question looks like is for GridLayout only.
The four buttons will be added in one row, thus making accommodation to the extra buttons.
I agree. Here's the link:
subject: GridBagLayout problem
jQuery in Action, 3rd edition