Newbie: problem with GridLayout

Z

ZalekBloom

I am testing a GridLayout. Here is my program:

package Layouts;
import java.awt.* ;

public class test2 extends Frame {
public static void main(String[] args) {
GridLayout gl = new GridLayout(8,3);
Button b1 = new Button("Button 1") ;
Button b2 = new Button("Button 2") ;
Button b3 = new Button("Button 3") ;
Button b4 = new Button("Button 4") ;
Button b5 = new Button("Button 5") ;
Button b6 = new Button("Button 6") ;
Button b7 = new Button("Button 7") ;
Button b8 = new Button("Button 8") ;
Button b9 = new Button("Button 9") ;
test2 f = new test2() ;

f.setLayout(gl);
f.add(b1);
f.add(b2);
f.add(b3);
f.add(b4);
f.add(b5);
f.add(b6);
f.add(b7);
f.add(b8);
f.add(b9);

f.setSize(1000,1000);
f.setVisible(true);

}
}

I played with the statement: GridLayout gl = new GridLayout(8,3);
I noticed that if I am using GridLayout(9,3) - I see one column of 9 rows.
When I am using GridLayout(8,3) - I see 2 columns and 5 rows with buttons arranged:
1,2
3,4
5,6
7,8
9
My question: I understood froman old book it should be:
1,2,3
4,5,6
7,8,9

Where I can find an explanation about bottons position?

Thanks,

Zalek
 
A

Andrew Thompson

| I am testing a GridLayout. Here is my program:

| GridLayout gl = new GridLayout(8,3);

Change that to..
GridLayout gl = new GridLayout(3,0);
for the desired effect.
 
A

Andrew Thompson

| what happens when you take your f.setSize(1000,1000) out? -Ike

It get's smaller. ...A lot smaller.
I had to take the damn thing out
before I could tolerate dealing
with the code.

The OP likes BIG buttons. ;-)
 
T

Tony Morris

A couple of general rules for GridLayout:
- ALL components will be the same size as each other, no matter what.
- The total number of components should be equal to the product of the
number of rows and the number of columns.

In the suggested case, this is 3 (rows) and 0 (columns) respectively, whch
gives a product of 0 (3 * 0 == 0). This is absolutely fine if you are going
to add zero components to your GridLayout, but I suspect otherwise.

There is a layout manager tutorial on http://java.sun.com/tutorial if I
remember erectly.

--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)
 
A

Andrew Hobbs

Tony Morris said:
A couple of general rules for GridLayout:
- ALL components will be the same size as each other, no matter what.
- The total number of components should be equal to the product of the
number of rows and the number of columns.

In the suggested case, this is 3 (rows) and 0 (columns) respectively, whch
gives a product of 0 (3 * 0 == 0). This is absolutely fine if you are going
to add zero components to your GridLayout, but I suspect otherwise.

Not so.
There is a layout manager tutorial on http://java.sun.com/tutorial if I
remember erectly.

If you look at the documentation for GridLayout the following paragraph is
in the explanation at the top

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.

This is all you need to know to work out how the manager works.

Cheers

********************************************************
Andrew Hobbs PhD

MetaSense Pty Ltd - www.metasense.com.au
12 Ashover Grove
Carine W.A.
Australia 6020

61 8 9246 2026
(e-mail address removed)

*********************************************************

--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)


Andrew Thompson said:
| I am testing a GridLayout. Here is my program:

| GridLayout gl = new GridLayout(8,3);

Change that to..
GridLayout gl = new GridLayout(3,0);
for the desired effect.

--
Andrew Thompson
* http://www.PhySci.org/ PhySci software suite
* http://www.1point1C.org/ 1.1C - Superluminal!
* http://www.AThompson.info/andrew/ personal site
 
T

Tony Morris

right, thus I mentioned it as a "general rule"

--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)


Andrew Hobbs said:
Tony Morris said:
A couple of general rules for GridLayout:
- ALL components will be the same size as each other, no matter what.
- The total number of components should be equal to the product of the
number of rows and the number of columns.

In the suggested case, this is 3 (rows) and 0 (columns) respectively, whch
gives a product of 0 (3 * 0 == 0). This is absolutely fine if you are going
to add zero components to your GridLayout, but I suspect otherwise.

Not so.
There is a layout manager tutorial on http://java.sun.com/tutorial if I
remember erectly.

If you look at the documentation for GridLayout the following paragraph is
in the explanation at the top

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.

This is all you need to know to work out how the manager works.

Cheers

********************************************************
Andrew Hobbs PhD

MetaSense Pty Ltd - www.metasense.com.au
12 Ashover Grove
Carine W.A.
Australia 6020

61 8 9246 2026
(e-mail address removed)

*********************************************************

--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)


Andrew Thompson said:
| I am testing a GridLayout. Here is my program:

| GridLayout gl = new GridLayout(8,3);

Change that to..
GridLayout gl = new GridLayout(3,0);
for the desired effect.

--
Andrew Thompson
* http://www.PhySci.org/ PhySci software suite
* http://www.1point1C.org/ 1.1C - Superluminal!
* http://www.AThompson.info/andrew/ personal site
 
A

Andrew Hobbs

Tony Morris said:
right, thus I mentioned it as a "general rule"

Your first point is OK. But not the second.

With Grid Layout there is no point in specifying both the number of rows AND
the number of columns. If you specify the number of rows, the number of
columns is ignored completely, and might as well be set to 0 as anything
else. If you do not specify the number of rows then and only then do you
need to specify the number of columns.

You say

"The total number of components should be equal to the product of the number
of rows and the number of columns."

Which is incorrect. For example, if you have 12 components and you want a
3 x 4 matrix then (3, 0) is a perfectly reasonable way of doing it, as is
(0, 4). (3, 4) will also work, although (3, 5), (3, 12) etc will also work
because the second number is completely ignored.

Note that (3, 0) and (0, 4) will not give the same results with different
numbers of components. As you add more and more components to a panel with
a GridLayout(3, 0) you will get a matrix of components with 3 horizontal
rows and a variable number of columns. With GridLayout(0, 4) you will end
up with a matrix with 4 columns and a variable number of rows.

Cheers

********************************************************
Andrew Hobbs PhD

MetaSense Pty Ltd - www.metasense.com.au
12 Ashover Grove
Carine W.A.
Australia 6020

61 8 9246 2026
(e-mail address removed)

*********************************************************



--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)


Andrew Hobbs said:
Tony Morris said:
A couple of general rules for GridLayout:
- ALL components will be the same size as each other, no matter what.
- > > >
In the suggested case, this is 3 (rows) and 0 (columns) respectively, whch
gives a product of 0 (3 * 0 == 0). This is absolutely fine if you are going
to add zero components to your GridLayout, but I suspect otherwise.

Not so.
There is a layout manager tutorial on http://java.sun.com/tutorial if I
remember erectly.

If you look at the documentation for GridLayout the following paragraph is
in the explanation at the top

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.

This is all you need to know to work out how the manager works.

Cheers

********************************************************
Andrew Hobbs PhD

MetaSense Pty Ltd - www.metasense.com.au
12 Ashover Grove
Carine W.A.
Australia 6020

61 8 9246 2026
(e-mail address removed)

*********************************************************

--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)


| I am testing a GridLayout. Here is my program:

| GridLayout gl = new GridLayout(8,3);

Change that to..
GridLayout gl = new GridLayout(3,0);
for the desired effect.

--
Andrew Thompson
* http://www.PhySci.org/ PhySci software suite
* http://www.1point1C.org/ 1.1C - Superluminal!
* http://www.AThompson.info/andrew/ personal site
 
T

Tony Morris

ok.

--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)


Andrew Hobbs said:
Tony Morris said:
right, thus I mentioned it as a "general rule"

Your first point is OK. But not the second.

With Grid Layout there is no point in specifying both the number of rows AND
the number of columns. If you specify the number of rows, the number of
columns is ignored completely, and might as well be set to 0 as anything
else. If you do not specify the number of rows then and only then do you
need to specify the number of columns.

You say

"The total number of components should be equal to the product of the number
of rows and the number of columns."

Which is incorrect. For example, if you have 12 components and you want a
3 x 4 matrix then (3, 0) is a perfectly reasonable way of doing it, as is
(0, 4). (3, 4) will also work, although (3, 5), (3, 12) etc will also work
because the second number is completely ignored.

Note that (3, 0) and (0, 4) will not give the same results with different
numbers of components. As you add more and more components to a panel with
a GridLayout(3, 0) you will get a matrix of components with 3 horizontal
rows and a variable number of columns. With GridLayout(0, 4) you will end
up with a matrix with 4 columns and a variable number of rows.

Cheers

********************************************************
Andrew Hobbs PhD

MetaSense Pty Ltd - www.metasense.com.au
12 Ashover Grove
Carine W.A.
Australia 6020

61 8 9246 2026
(e-mail address removed)

*********************************************************



--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)


Andrew Hobbs said:
A couple of general rules for GridLayout:
- ALL components will be the same size as each other, no matter what.
- > > >
In the suggested case, this is 3 (rows) and 0 (columns)
respectively,
whch
gives a product of 0 (3 * 0 == 0). This is absolutely fine if you are
going
to add zero components to your GridLayout, but I suspect otherwise.


Not so.

There is a layout manager tutorial on http://java.sun.com/tutorial
if
paragraph
is number
of
number
of
columns affects the layout only when the number of rows is set to zero.

This is all you need to know to work out how the manager works.

Cheers

********************************************************
Andrew Hobbs PhD

MetaSense Pty Ltd - www.metasense.com.au
12 Ashover Grove
Carine W.A.
Australia 6020

61 8 9246 2026
(e-mail address removed)

*********************************************************


--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)


| I am testing a GridLayout. Here is my program:

| GridLayout gl = new GridLayout(8,3);

Change that to..
GridLayout gl = new GridLayout(3,0);
for the desired effect.

--
Andrew Thompson
* http://www.PhySci.org/ PhySci software suite
* http://www.1point1C.org/ 1.1C - Superluminal!
* http://www.AThompson.info/andrew/ personal site
 
A

Andrew Hobbs

Tony Morris said:

One point that I missed is that the points I made are valid for dividing up
the panel and deciding the size of the sub Components, BUT, the arrangement
of the components involves filling the matrix a row at a time. This can
give rise to several empty rows at the bottom of the panel.

If a panel has 9 components added and has the layout set to GridLayout(8, 3)
then the manager works out that 2 columns are required to accommodate the 9
components. (ie it has 16 spaces) It then proceeds to fill in the rows
from the top giving 4 filled rows and one extra button in the fifth row.
This will leave 3 1/2 empty rows at the bottom.

Cheers

Andrew

********************************************************
Andrew Hobbs PhD

MetaSense Pty Ltd - www.metasense.com.au
12 Ashover Grove
Carine W.A.
Australia 6020

61 8 9246 2026
(e-mail address removed)

*********************************************************
 

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,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top