Friday, February 06, 2009

GridbagLayout anchor ignored

I banged my head at this all morning... I hope you dont have to go through this so posting it here.
The problem is that in some cases the GridBagConstraints.anchor setting is ignored and I couldnt quite figure out why. Until I read this in the javadocs :
weightx, weighty
Specifying weights is an art that can have a significant impact on the appearance of the components a GridBagLayout controls. Weights are used to determine how to distribute space among columns (weightx) and among rows (weighty); this is important for specifying resizing behavior.

Unless you specify at least one non-zero value for weightx or weighty, all the components clump together in the center of their container. This is because when the weight is 0.0 (the default), the GridBagLayout puts any extra space between its grid of cells and the edges of the container.

Now if you do not specify weight params, the anchor setting seems to be ignored completely. I dont know why. Also I have noticed in some other cases that the anchor works despite the weight params not being set. In those cases the ipad constraints were set. So it could be that setting ipad also works.