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
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.
<BLOCKQUOTE><font size="1" face="Verdana, Arial">quote:</font><HR>"Those who cast the votes decide nothing. Those who count the<BR>votes decide<BR>everything." <BR> -Joseph Stalin<HR></BLOCKQUOTE>
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