• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

GridLayout Manager Doubt

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My doubt is regarding gridlayout manager.
What happens when number of components added to container is more than the number specified in constructor.I know that the additional components get accomodated but what rule is followed for exact positioning of components
I also think that the GridLayout manager always respects the number of rows specified in the constructor.Is that correct?
Thanks in advance
Jayram
------------------
 
Ranch Hand
Posts: 1871
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The GridLayout constructor is defined as
GridLayout(int rows, int cols) Creates a grid layout with the specified number of rows and columns.

First the rows are being filled as the components are added and later on the columns.
In an example program below I have created a GridLayout of size 2 X 2. However i go on to add 8 buttons. what happens?? . Columns are added not rows.
Thus let us answer your questions one by one
Question 1)
What happens when number of components added to container is more than the number specified in constructor.
The number of columns increase to acommodate the components added , but the number of rows remain the same.
Question 2.
I know that the additional components get accomodated but what rule is followed for exact positioning of components.
Ans. check out the program below and see the sequence of the components added. Basically it would be as if the GridLayout knew beforhand the total number of components to be added and it adjusts the components in a sequence.
Question 3.
I also think that the GridLayout manager always respects the number of rows specified in the constructor.Is that correct?
Ans. Yes you are absolutely right

Regds.
Rahul
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The grid layout has the following properties:
1. GridLayout(a, 0) and GridLayout(a, b) do the same job
if both a and b are int type and a > 0;
2. GridLayout(0, b) and GridLayout(a, b) do the same job
if both a and b are int type and b > 0;
3. GridLayout() and GridLayout(1, 0) do the same job

4. GridLayout(0,0) generates a run time exception.



 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
DDDD- your second statement is false, as it contradicts the first. What you probably mean to say is that the only way b will ever matter is if a is set equal to zero, in which case the number of columns will be equal to b and the number of rows will be whatever is necessary to fit the number of components. In other words,
<code>new GridLayout(0, b)</code>
is equivalent to
<code>new GridLayout(Math.ceil( ((float) n)/b ), b)</code>
where n is the number of components added to the container.
 
Rahul Mahindrakar
Ranch Hand
Posts: 1871
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
DDDD has raized some intresting things
1. GridLayout(0,0) generates a run time exception. (true)
2 GridLayout(a, 0) : where a>0 In this case the rows remain constant at a while the columns are added whenever a component is added that is greater than a
3. GridLayout(0, b) : In this case the columns remain constant at b and the rows are increased whenever a component is added that is greater than b.
4. GridLayout(a,b) where a >0 and b > 0 then the behaviour of the layout is similar to case 3 wherein the columns are increased to acommodate components added and the rows remain the same as a.
5. GridLayout() and GridLayout(1, 0) do the same job
Regds.
Rahul
[This message has been edited by Rahul Mahindrakar (edited August 07, 2000).]
 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rahul,
your example(GridLayout(2,2))you told that , the number of
rows remain same if we add more components. which is contradicting with the following rule...
GridLayout(a,b) where a >0 and b >= 0 then the behaviour of the layout is similar to case 2 wherein the rows are
increased to acommodate components added and the columns remain the same.
- Thanks
 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
GridLayout uses the following algorithm to decide the number of rows and columns:

if (nrows > 0) {
ncols = (ncomponents + nrows - 1) / nrows;
else
nrows = (ncomponents + ncols - 1) / ncols;

If nrows is greater than zero, a new value of ncols is computed based on the specified nrows and the number of components. If nrows is zero, a new value of nrows is computed based on the specified ncols (which cannot then be zero) and the number of components.
So there is always sufficient number of cells for all the components. The components are laid out left to right starting with the top row.

[This message has been edited by thomas (edited August 06, 2000).]
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import java.awt.event.*;
import java.awt.*;
public class JxFrame extends Frame
{
public JxFrame(String title)
{
super(title);
setCloseClick();
setLayout(new GridLayout(2,2));
add(new Button("1"));
add(new Button("2"));
add(new Button("3"));
add(new Button("4"));
add(new Button("5"));
add(new Button("6"));
add(new Button("7"));
add(new Button("8"));
add(new Button("8")); ///see here...........
}
public static void main(String[] args)
{
JxFrame x = new JxFrame("Demo Application");
x.setSize(200,200);
x.show();
System.out.println("Hello World!");
}
private void setCloseClick()
{
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){ System.exit(0);} });
}
}
Its not the part of the above question but i tried something else with the above code if you see i have add a line
add(new Button("8"));
and the end....now the question is.....
will the above code compile.......according to me it should give a runtime exception ...but the thing is thatt it compiles and runs perfectly fine......can anyone tell me the reason....
Thanx and bye......
 
Rahul Mahindrakar
Ranch Hand
Posts: 1871
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Doit,
Thanks and good work. things stand rectified in my earlier post.
Sadashiv Borkar, there is a difference between
add(new Button("b"));
add(new Button("b"));
and
Button b=new Button("b");
add(b);
add(b);
in the former case two button instances are created and are added to the frame.
In the latter case only one instance of a button exists in memory. Adding the same button twice does not yield 2 buttons on the frame and only one button is added into the frame in the latter case.

Regds.
Rahul.

[This message has been edited by Rahul Mahindrakar (edited August 07, 2000).]
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
There are three forms of constructors for GridLayout
(1) public GridLayout()
Creates a grid layout with a default of one column per component, in a single row.
(2) public GridLayout(int rows,int cols)
Creates a grid layout with the specified number of rows and columns. All components in the layout are given equal size.
One, but not both, of rows and cols can be zero, which means that any number of objects can be placed in a row or in a column.
rows - the rows, with the value zero meaning any number of rows.
cols - the columns, with the value zero meaning any number of columns.
Hope this is clear. You can refer about this at http://java.sun.com/products/jdk/1.2/docs/api/index.html
 
reply
    Bookmark Topic Watch Topic
  • New Topic