When you use the Swing GridBag layout, the visual editor sets default constraints on components. You can change these constraints using the Properties view or the Customize Layout dialog.
Constraints, such as fill, anchor, weight x, and weight y, are set based on the component type in order to display the component better on a GridBagLayout. For example, when you drop a javax.swing.JTextField, the fill is set to HORIZONTAL (default value for JTextField is NONE), and the weight x value is set to 1 (default for JTextField value is 0). Otherwise, the JTextField is barely visible.
In the following example, the JScrollBar was dropped with a fill value set to VERTICAL and the weight y value set to 1.
The generated constraint for a new component instantiates GridBagConstraints using its null constructor.
When switching to a GridBagLayout from another layout manager, the visual editor for Java will generate constraints for each component based on their existing bounds, and the net effect will be that the components do not move their position. Instead of being placed there by an absolute rectangle such as a container with a layout set to null, they have GridBagConstraints.
The advantage of using GridBagConstraints instead of a null layout is that each component is sized according to its preferred size. So, if the size of the parent window changes or if the preferred sizes change at run time due to different language strings, the layout manager will resize and reposition all of the components and make the best use of the available space.
To specify anchor, fill, and inset constraints for a component in GridBag layout: