aspose file tools*
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
Author

GridBagLayout problem

ZEESHAN AZIZ
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"));
}
}

##ans1##
The Gridlayout is created with 1 row and 2 columns.
##ans2##
Adding the button with the label "#3" will cause an exception to be thrown.
##ans3##
Adding the button with label "#3" will cause it to overwrite the button with label "#2".
##ans4##
The layout automatically extends to accommodate the additional buttons.
##ans5##
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.
/Mike
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.

Mike
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.

Zeeshan
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
Dave

Dave
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
Greenhorn

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: http://aspose.com/file-tools
 
subject: GridBagLayout problem