Setting the Stretchyness of a Component Within the Cell of a GridBagLayout Using Fill

By default, when the cell is larger than the preferred size of a component, the component is centered within the cell. You can stretch a component horizontally or vertically, or in both directions within the cell by setting its fill constraint.

See Creating a GridBagLayout for an example on how to use a gridbag layout with gridbag constraints.

GridBagLayout gbl = new GridBagLayout(); container.setLayout(gbl); GridBagConstraints gbc = new GridBagConstraints(); // Make the component on stretchable gbc.fill = GridBagConstraints.NONE; // Make the component only stretch horizontally gbc.fill = GridBagConstraints.HORIZONTAL; // Make the component only stretch vertically gbc.fill = GridBagConstraints.VERTICAL; // Make the component stretch in both directions gbc.fill = GridBagConstraints.BOTH; gbl.setConstraints(component, gbc); container.add(component);

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.