• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

gridBagLayout

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic