Search code examples
javaswinglayout-managergridbaglayout

GridBagConstraints filling both VERTICAL & HORIZONTAL


How do I go about filling both the vertical and horizontal space available in the grid cell of interest?

For example:

JTextArea file1 = new JTextArea();
file1.setBorder(BorderFactory.createLineBorder(Color.black));
c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 1;
c.weightx = 1.0 / ELEMENTS_IN_WIDTH;
c.weighty = 0.9;
c.insets = new Insets(PAD, PAD, PAD, PAD);
c.fill = GridBagConstraints.VERTICAL;
mainPanel.add(file1, c);

This fills the cell vertically but not horizontally.

Obviously adding c.fill = GridBagConstraints.HORIZONTAL after the VERTICAL value will only override the value of c.fill

OR'ing them like c.fill = GridBagConstraints.HORIZONTAL | GridBagConstraints.VERTICAL also doesn't do the trick.


Solution

  • You should use:

    GridBagConstraints gridConst = new GridBagConstraints();
    gridConst.fill = GridBagConstraints.BOTH;