Search code examples
javaswingawtlayout-managergridbaglayout

GridBagLayout weightx and weighty specification


I have a JPanel that I have specified as having GridBagLayout. The grid that I have drawn (on paper) is 8x8, and I have all the components drawn out as I would like them to appear.

For each component, I specified its gridx, gridy, gridwidth, and gridheight, but I am unsure of what to do about the weightx and weighty. All the 'cells' in the 8x8 grid are the same size, but I used the gridwidth and gridheight to specify how many cells each component takes up, and gridx and gridy to specify their top-left corner.

How do I use the weightX and weightY?


Solution

  • Read the Swing tutorial on How to Use Grid Bag Layout. weightx/y are used to determine how extra space is allocated to components.

    If you you don't want the components to resize leave them at 0. If you want them to increase with the frame size set them to 1.