• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

GridBag and GridBagConstraints

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am having a really difficult time understanding the relationship between the GridBag layout manager and the GridBagConstraints class. The GridBag layout manager seems so difficult to use that it almost isn't worth the effort.
My questions are:
1. Is the only way to add components to a cell in the GridBag layout by using a GridBagConstraints object?
2. Is it just me, or wouldn't it be easier for the coder to remember where the cells in the GridBag are and just pass coordinates to the add() method instead of an object reference?
Does anyone know of a resource that would help me understand this layout manager? Or maybe explain it in simpler terms?
Thanks,
Brian
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I've read everything on the market on GBs and created quite a few layouts with them, and they are still very unpredictable. One good thing, tho. The default values of the constraints are pretty useful. If you add your components left to right, top to bottom, the only value you need to change is gridwidth, which should be set to .REMAINDER for the last component in each row, and back to 1 at the beginning of the next row (they neglect to mention this in the docs). One thing to realize is that you can use the same constraints for all your components (i.e.
panel.add(comp1, bag);
bag.gridwidth = GridbagConstraints.REMAINDER;
panel.add(comp2, bag);
There is a way cool GB testing app in Professional Java Programming (thanks to JavaRanch and WROX!) which lets you change constraints in real time, and see what happens to your layout. I hope to figure out more from that.
It would be nice to have a layout that would take an x and a y coordinate, like GridLayout. I may try to create a GridPanel object that works that way.
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I use Oracl'es JDEveloper for creating layouts.
I first use the XYLayout manager to create a layout (really easy to use, you can drag and drop text fields, button ...) and finally a switch to Gridbaglayout manager (by selecting this manager from a seleciton menu).
JDeveloper then automatically changes the code so using the Gridbaglayout class and the components stay were you defined them.
Really cool ! No pain at all to define a layou, just drag and drop !
Regards,
Stefan
 
Slime does not pay. Always keep your tiny ad dry.
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic