This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

GridBagLayout problem

 
ZEESHAN AZIZ
Ranch Hand
Posts: 162
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 158
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3244
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Paul Michael
Ranch Hand
Posts: 697
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ji Wang
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic