grid bag layout problem

G

gk

Frame f = new Frame("Question 3");
f.setLayout(new GridLayout(2,3));
for(int i=0;i<(int)Math.round(6.51);i++){
f.add(new Button(""+(i+1)));
}
f.pack();
f.validate();
f.setVisible(true);

the output is not understable.

the output button is

1 2 3 4
5 6 7


Question :

why the output is like this ?

see, we have GridLayout(2,3) , this means its a 2x3 grid .....right ?
so basically there should be 6 cells...like below

c1 c2 c3
c4 c5 c6

and the button should be placed serially on these cells...and as a
result we should not see also the 7th button.

so, my proposed solution is :
1 2 3
4 5 6



Now tell me, where is the mistake in my solution......why the real
solution does not match with my answer ?
 
A

Andrew Thompson

gk said:
Frame f = new Frame("Question 3");
f.setLayout(new GridLayout(2,3));

Note that GridLayout and GridBagLayout are separate
and distinct layouts (and classes).
for(int i=0;i<(int)Math.round(6.51);i++){

Perhaps there is a higher purpose to the statement ..
i<(int)Math.round(6.51)
...but I will point out that it is easier to write instead..
i<7
f.add(new Button(""+(i+1)));
}
f.pack();
f.validate();
f.setVisible(true);

the output is not understable.

the output button is

1 2 3 4
5 6 7

That equeste to your code, yes.
Question :

why the output is like this ?

The loop goes from 0-6 inclusive - a total of
seven numbers.
see, we have GridLayout(2,3) , this means its a 2x3 grid .....right ?

Yes. But once you feed it more than 6 components, ..
"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."
Now tell me, where is the mistake in my solution......why the real
solution does not match with my answer ?

The loop runs 7 times.

Andrew T.
 
G

gk

Yes. But once you feed it more than 6 components, ..
"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."


not clear here what u mentioned..

but why its like this ?

1 2 3 4
5 6 7





WHY NOT like below ?

1 2 3
4 5 6 7


OR WHY NOT like below

1 2 3 4 5 6
7


Please note , the above two proposed solution ignoring the column as
you mentioned in your comment.....but unfortunately these are not
solutions!.........why ?
 
A

Andrew Thompson

Yes ..what? Please refrain from top-posting,
I find it very confusing.

Andrew T.
 
G

gk

Hi i copied some of your comment and posted my comment on that .

so i did that way.

As there is no quote kind of facility....so i did that way....that was
a specific query based upon your specific comment.

whats the solution for this ?

did you understand what i posted in my comment ....If not please
respond .....i'll re-explain.
 
A

Andrew Thompson

gk said:
Hi i copied some of your comment and posted my comment on that .
so i did that way. ....

Please google top-posting before making more posts.
I would like to get that matter sorted, before proceeding.

(Hint: you are continuing to top-post.)

Andrew T.
 
G

gk

..

BTW, this time i am simply pasting my response here....probabily this
would be ok now.


but why the solution is like this ?

1 2 3 4
5 6 7

WHY NOT like below ?

1 2 3
4 5 6 7

OR WHY NOT like below

1 2 3 4 5 6
7


I hope you will come back to the discussion now and leave out this
thingie.

what i am doing here is, i am pressing "Reply" button in my Firefox
browser and posting you........anyway, can we please come back to the
discussion again ?

thanks
 
M

Matt Humphrey

gk said:
.

BTW, this time i am simply pasting my response here....probabily this
would be ok now.

Top-posting means you are putting the quoted section on the bottom and your
comments on the top. Notice how I've quoted your section onto the top and
put my comments underneath. Is the quoted section visible when you edit the
message or is your newsreader appending it automatically?
but why the solution is like this ?

1 2 3 4
5 6 7

The answer you're looking for is in the API, which you can find here
http://java.sun.com/j2se/1.5.0/docs/api/ and is always a great place to
start for figuring out how things work. Specifically, GridLayout says:
"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, 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. "

So when you say new GridLayout (2, 3) the 3 is ignored. 4 columns are
needed to hold the 7 values, with the first row having the extra item.

Matt Humphrey (e-mail address removed) http://www.iviz.com/
 
O

Oliver Wong

[post re-ordered for clarity]

gk said:
I hope you will come back to the discussion now and leave out this
thingie.

what i am doing here is, i am pressing "Reply" button in my Firefox
browser and posting you........anyway, can we please come back to the
discussion again ?

Some people will refuse to help you if you top-post. If you want to maximize
the number of helpful answers, avoid top posting. You should read the
following documents:

http://www.faqs.org/faqs/usenet/posting-rules/part1/
http://www.faqs.org/faqs/usenet/writing-style/part1/
http://www.faqs.org/faqs/usenet/primer/part1/
http://www.faqs.org/faqs/usenet/faq/part1/
but why the solution is like this ?

1 2 3 4
5 6 7

WHY NOT like below ?

1 2 3
4 5 6 7

OR WHY NOT like below

1 2 3 4 5 6
7

See http://java.sun.com/j2se/1.5.0/docs/api/java/awt/GridLayout.html
<quote>
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, 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.
</quote>

So in other words, if you lie about the number of components, GridLayout
is free to do whatever it wants, it seems.

- Oliver
 
O

Oliver Wong

Matt Humphrey said:
Top-posting means you are putting the quoted section on the bottom and
your comments on the top. Notice how I've quoted your section onto the
top and put my comments underneath. Is the quoted section visible when you
edit the message or is your newsreader appending it automatically?

OP is posting from Google Groups.

- Oliver
 
G

gk

Matt said:
"


Top-posting means you are putting the quoted section on the bottom and your
comments on the top. Notice how I've quoted your section onto the top and
put my comments underneath. Is the quoted section visible when you edit the
message or is your newsreader appending it automatically?


The answer you're looking for is in the API, which you can find here
http://java.sun.com/j2se/1.5.0/docs/api/ and is always a great place to
start for figuring out how things work. Specifically, GridLayout says:
"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, 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. "

So when you say new GridLayout (2, 3) the 3 is ignored. 4 columns are
needed to hold the 7 values, with the first row having the extra item.



why 4 columns are needed to hold 7 values ? as you told....in 2,3
.....the column 3 will be ignored......well ok......so there might be 4
, 5 ,6 ,7 or whatever , any number column could be generated at
runtime.....right ? .

but why you are stick to column 4 ? is there any restriction tha
generated column no=ignored column number +1 = 3+1=4

is that formula holds ?

you told we have 7 items...fine.....to hold seven items any of the
following is enough ..



1 2 3 4 5 6
7

OR

1 2 3 4 5 6 7

see, in all these PROBABLE output column number 3 has been ignored
......the first output has 6 columns and second output has 7 columns.

can you please give me deterministic formula how many column would be
generated ?
 
M

Matt Humphrey

Matt Humphrey wrote:

why 4 columns are needed to hold 7 values ? as you told....in 2,3
....the column 3 will be ignored......well ok......so there might be 4
, 5 ,6 ,7 or whatever , any number column could be generated at
runtime.....right ? .

but why you are stick to column 4 ? is there any restriction tha
generated column no=ignored column number +1 = 3+1=4

is that formula holds ?

you told we have 7 items...fine.....to hold seven items any of the
following is enough ..



1 2 3 4 5 6
7

OR

1 2 3 4 5 6 7

see, in all these PROBABLE output column number 3 has been ignored
.....the first output has 6 columns and second output has 7 columns.

can you please give me deterministic formula how many column would be
generated ?

The API says that the number of columns is derived from the number of rows
and the total, which suggests its the minimum number of columns needed to
get the job done and there is never more than one partially filled column
and never a completely empty column.

numberOfColumns = 1 + Floor ((numberOfItems - 1) / numberOfRows)

It also says the items layout out first by columns, which gives for any
itemNumber 0, 1, 2, ...

row = Floor (itemNumber / numberOfColumns)
col = itemNumber % numberOfColumns

Matt Humphrey (e-mail address removed) http://www.iviz.com/
 
G

gk

still its confusing.

Matt said:
The API says that the number of columns is derived from the number of rows
and the total, which suggests its the minimum number of columns needed to
get the job done and there is never more than one partially filled column
and never a completely empty column.

numberOfColumns = 1 + Floor ((numberOfItems - 1) / numberOfRows)

It also says the items layout out first by columns, which gives for any
itemNumber 0, 1, 2, ...

row = Floor (itemNumber / numberOfColumns)
col = itemNumber % numberOfColumns

Matt Humphrey (e-mail address removed) http://www.iviz.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top