title: Java GridBagLayout - disable vertical fill
body: <p>I'm having a problem with vertical filling in my GridBagLayout-GUI. Here is the Code:</p>
  
  <pre><code>import java.awt.BorderLayout;
  import java.awt.Dimension;
  import java.awt.GridBagConstraints;
  import java.awt.GridBagLayout;
  import java.awt.Insets;
  
  import javax.swing.JFrame;
  import javax.swing.JLabel;
  import javax.swing.JPanel;
  import javax.swing.JScrollPane;
  import javax.swing.JTextField;
  
  public class Spielklasse {
  
  public static void main(String[] args) {
  
      JPanel page = new JPanel();
      page.setLayout(new GridBagLayout());
  
      for (int i = 0; i &lt; 5; i++) {
          JPanel subPanel = new JPanel(new BorderLayout());
          JLabel label = new JLabel();
          label.setText("label " + i + ":");
          JTextField textField = new JTextField();
          textField.setPreferredSize(new Dimension(120,20));
  
          subPanel.add(label, BorderLayout.WEST);
          subPanel.add(textField, BorderLayout.EAST);
  
          GridBagConstraints c = new GridBagConstraints();
          c.anchor = GridBagConstraints.NORTHWEST;
          c.gridx = 1;
          c.gridy = i;
          c.weightx = 1;
          c.weighty = 1;
          c.insets = new Insets(10, 20, 10, 20);
          c.fill = GridBagConstraints.HORIZONTAL;
          page.add(subPanel, c);
      }
  
      JScrollPane scrollPane = new JScrollPane(page);
  
      JFrame frame = new JFrame();
      frame.setSize(300,500);
      frame.add(scrollPane);
      frame.setVisible(true);
  }
  }
  </code></pre>
  
  <p>I want to have label+textfield directly under the previous ones without filling the vertical space between (like when you resize my example vertically till you can see the vertical scrollbar). How can I achieve that? With my code the whole panel is filled vertically, but I don't want this.</p>