Two Laptop Bag*
The moose likes Swing / AWT / SWT and the fly likes gridBagLayout Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "gridBagLayout" Watch "gridBagLayout" New topic
Author

gridBagLayout

kate damond
Ranch Hand

Joined: Apr 17, 2001
Posts: 77
Hi,
I thought I had understood this whole gridbaglayout thing but clearly not ... I have 4 empty panels arranged on a contentPane. LayoutManager is GridBagLayout, and I think I should end up with 2 columns and four rows, with each component 1 column wide, but different rows high :

Top Left Panel is
gridx = 0, gridy = 0,
gridwidth= 1, gridHeight = 2
Lower Left Pane is
gridx = 0, gridy = 2,
gridwidth= 1, gridHeight = 2
So I expect these two to be the same height and they are.
Then, in the second column I have
Top Right Panel is
gridx = 1, gridy = 0,
gridwidth= 1, gridHeight = 1
Lower Right Panel is
gridx = 1, gridy = 1,
gridwidth= 1, gridHeight = 3
Now, because I set the relative heights as stated, I was expecting the lower right panel to be taller than the Top right Panel. However, all four panels are the same height. What am I doing wrong please ? (I have made sure all 4 components are exactly the same type - they are empty JPanels, and I am working in Visual Age's visual Composition editor)
Many Thanks,
kate
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Kate,
You have stumbled onto something very strange indeed. I am at a loss to explain the behavior. But, I do know that when you are working with GridBagLayouts you need to set the weightx and weighty fields of the GridBagConstraints class. In your case, to get the panels to layout the way you want them (I think!), you need to set all the left weighty fields to a value such as 1.0. Then for the upper right one make it zero. Here is the part I can't explain: if you make the upper right panel weighty constraint anything other than 0.0 the layout screws up. I am not exactly sure why that is because the only thing I thought was important is that all the weighty fields be equal in any direction! For the lower right you should make weighty = 2.0 so that at least in theory it make the weighty values total the same:
Left: 1 + 1
Right: 0 + 2
Hope it helps,
Manfred.
Nidhi Madan
Greenhorn

Joined: May 09, 2001
Posts: 9
Hi Kate,
the problem u r facing is a common one with gridbaglayout. u r not doing anything wrong, what happens in gridbaglayout is that the gridwidth and gridheight attributes are measured in terms of the other components on the container.
since u have given ur components different widths and heights, but they r having the same attributes(i am not talking about gridbagconstraints) they have the same 2D dimensions. hence even when u give them gridbag constraints, they try to adjust according to the container and fellow components and what u get is same sized components.

expected solution:
try putting the top-right and lower-right panels in a container and then put this container in ur original container.

PUT THESE IN ORIGINAL CONTAINER
{
Top Left Panel is
gridx = 0, gridy = 0,
gridwidth= 1, gridHeight = 1
Lower Left Pane is
gridx = 0, gridy = 1,
gridwidth= 1, gridHeight = 1
}
PUT THESE IN A NEW CONTAINER
{
Top Right Panel is
gridx = 0, gridy = 0,
gridwidth= 1, gridHeight = 1
Lower Right Panel is
gridx = 0, gridy = 1,
gridwidth= 1, gridHeight = 1
GIVE THESE PANELS INDIVIDUAL WEIGHTS(weightx and weighty) and fill as GridBagConstraints.BOTH
the weights should be such that one has greater weight than the other since they are calculated relatively, and u get the proportional result.
}
NOW ADD THIS NEW CONTAINER INTO UR ORIGINAL CONTAINER.
{
gridx = 1, gridy = 0,
gridwidth= 1, gridHeight = 2

}
kate damond
Ranch Hand

Joined: Apr 17, 2001
Posts: 77
Thank you so much for your help - I thought I was going barmy ! I've actually decided that gridbagLayout is a bit much of a handful for the short turn-around prototype that I'm doing (which can't be resized anyway) so I'm hardcoding everything's position and will come back to gridBagLayout when time is less of an issue - I have saved your answers though to learn from after the next deadline.
Many Thanks,
Kate
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: gridBagLayout