Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

GridBag and GridBagConstraints  RSS feed

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • 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
 
eric moon
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • 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.
 
Stefan Geelen
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!