File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Gridbag Layout Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Gridbag Layout" Watch "Gridbag Layout" New topic
Author

Gridbag Layout

Suresh
Ranch Hand

Joined: May 08, 2000
Posts: 76
hi,
(sorry, if it is not part of SCJP!! The book I have doesn't give much info on Gridbag layout)
If you set a value for the weightx in the first row, can't we change it for the second row? Suppose, I have two buttons in the first row with weightx 25 and 75. In the second row, I want to have two buttons with weightx 60 and 40.
I tried to set weightx for the second row components, but it was not working!!
Is it possible? How?
Thanks in advance,
Regards,
Suresh.
Suma Narayan
Ranch Hand

Joined: Apr 03, 2000
Posts: 136
Hi Suresh,
There is nothing to be sorry for, as GridBagLayout is very much part of SCJP.
You say:
I tried to set weightx for the second row components, but it was not working!!
Here, when you say it is not working, what exactly are the problems u are facing?? Is it giving compiler error or you are not seeing the required changes when you run the program.
Now, with respect to the second part of your forum:
Is it possible? How?
Yes, it is possible. I had very little knoweldge of GridBagLayout until you asked this question. In the past, I always postponed reading this topic as it is vast and confusing. But,Thanks to you that I tried to write the following program and it worked. Now I feel that I know more than before, may be not everything.It is not too confusing if one writes a program and experiments with it. I hope the following example helps you.
Note: In the example below, button b3 is starting in the new row when I reset the gidx constraint to 0. This will put the button at the start of the next row.

Try this link for GridBagLayout.
http://pandonia.canberra.edu.au/java/xadvisor/gridbag/gridbag.html

[
[This message has been edited by Suma Narayan (edited May 17, 2000).]
[This message has been edited by Suma Narayan (edited May 18, 2000).]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944

Suma:
good example. i do want to ask you a small favor....
( i am telling you before hand, i haven't done the gridbag
yet, i just bookmarked the above link, but if you have
time please .....)
i ran the program and saw 4 buttons in two rows.
Why two rows. where did we specify this ?
(If you feel you don't have time, never mind I WILL know
it the hard way.)
Thanks.
- satya
Suresh
Ranch Hand

Joined: May 08, 2000
Posts: 76
Suma,
Thanks a lot!!
Sorry for not being clear with my question!!
Actually, I create two rows of two buttons each.
In row1, button1 has weightx = 25 and button2 has weightx = 75.
In row2, button3 has weightx = 60 and button4 has weightx = 40.
What I expect is that in row1, button1 should be 1/4th the size of the whole row and in row2, button3 should be 3/5th of the row size.
But, what I get is:
If I set 25:75 weightx for row1 and 60:40 weightx for row2, Both the rows are displayed with 60:40 weightx. The 25:75 weightx ratio for row1 is not followed.
Here, I add my code.

import java.awt.*;
class GridBag2 extends Frame
{
void buildConstraints(GridBagConstraints gbc, int gx, int gy, int gw, int gh, int wx, int wy)
{
gbc.gridx = gx;
gbc.gridy = gy;
gbc.gridwidth = gw;
gbc.gridheight = gh;
gbc.weightx = wx;
gbc.weighty = wy;
}
GridBag2()
{
setSize(300, 200);
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
Button btn1 = new Button("Button 1");
buildConstraints(gbc, 0, 0, 1, 1, 30, 50);
add(btn1, gbc);
Button btn2 = new Button("Button 2");
buildConstraints(gbc, 1, 0, 1, 1, 70, 0);
add(btn2, gbc);
Button btn3 = new Button("Button 3");
buildConstraints(gbc, 0, 1, 1, 1, 60, 50);
add(btn3, gbc);
Button btn4 = new Button("Button 4");
buildConstraints(gbc, 1, 1, 1, 1, 40, 0);
add(btn4, gbc);
setVisible(true);
}
public static void main(String[] args)
{
GridBag2 gb2 = new GridBag2();
}
}

I faced the same problem with your code also. Please remove ipadx, ipady and insets values in your code and set constraints.fill = BOTH. And then execute the code.
I think I have put my question bit clear than before!!
And that link was great!! Thank you!!
Regards,
Suresh.
[This message has been edited by Suresh (edited May 18, 2000).]
Suma Narayan
Ranch Hand

Joined: Apr 03, 2000
Posts: 136
Hi Satya,
For the third button we have set constraints.gridx = 0. This puts button3 at the start of next row. gridx and gridy determines the position of the component in the container as coordinate positions of cell in the grid, where (0,0) is the top left position in the grid.
I hope this clears your doubt. I don't mind answering any type of questions. Everything adds to my knowledge. So, in case you want more explanation, let me know.
Suma.
Hi Suresh,
Let me try to run both programs and get back to you with the required solution.
Suma.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944

Thanks, Suma.
Now I know where to start and play with the code.
Regds.
- satya
Suma Narayan
Ranch Hand

Joined: Apr 03, 2000
Posts: 136
Hi Suresh,

  1. In both the programs, since the gridwidth and gridheight are set to 1 and it is same as that of button1, the buttons are displayed with the same size.(i.e size of the area occupied by them is same).
    This explains the same width and height of components.
  2. why weightx and weighty are not in the ratio of 25:75.

    I changed my program and took out the gridwidth and gridheight from Button3 conraints and gridx = RELATIVE and ran the program. This should'nt change the display from previous display, as the default value of gridwidth and gridheight is 1 and that of gridx is RELATIVE. But after changing, I can see the 25:75 ratio for button1 and button2.
    button3 occupies the entire width of row2 and button4 is below it.

  3. Probably, I am missing something. I would appreciate if others could add their comments/explanation to Suresh's question. Maha, we need your help.
    Here is the modified code:

    Thanks.
    Suma

    [This message has been edited by Suma Narayan (edited May 18, 2000).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Gridbag Layout
 
Similar Threads
GridBagLayout
specifying dimensions of JList
Gridbag layout
Size of a combobox
Mock exam