title: How to align the elements to the top in a GridBagLayout?
body: <p>I want to put a text element in a <code>GridBagLayout</code> after clicking on a button.
  But the elements are vertically centered. So how can I force the grid bag layout to position the elements to the top?</p>
  
  <p>Here is an example code:</p>
  
  <pre><code>import java.awt.*;
  import java.awt.event.*;
  import java.util.*;
  
  import javax.swing.*;
  
  public class RefreshPanel {
  
  private JFrame frame = new JFrame();
  private JPanel panel = new JPanel();
  private JTextPane [] textPane;// = new JTextPane[1];
  private JScrollPane scrollbar;
  private ArrayList arrayList = new ArrayList();
  private JButton newItem = new JButton("new");
  private GridBagLayout gbl = new GridBagLayout();
  private JMenu menuStart= new JMenu("Start");
  private JMenu menuEdit= new JMenu("Edit");
  private JMenu menuWindow= new JMenu("Window");
  private JMenu menuHelp= new JMenu("help");
  private JMenuBar menuBar = new JMenuBar();
  
  RefreshPanel() {
      menuBar.add(menuStart);
      menuBar.add(menuEdit);
      menuBar.add(menuWindow);
      menuBar.add(menuHelp);
      scrollbar = new JScrollPane(panel);
  
      panel.setBackground(Color.WHITE);
      panel.setLayout(gbl);                       
  
      addButtonListener();        
      createFrame();
  } //constructor
  
  public void addButtonListener() {
      newItem.addActionListener(new ActionListener() {
  
          @Override
          public void actionPerformed(ActionEvent arg0) {
              arrayList.add("data");
              textPane = generateTextPane(arrayList.size(), arrayList);
              System.out.println("Länge: "+textPane.length);
              for(int i=0;i&lt;textPane.length;i++) {
                  System.out.println(textPane[i].getText());
                  addComponent(panel, gbl, textPane[i], 0, i, 1, 1, 1, 0);
                  panel.revalidate();
              }       
              frame.revalidate();
          }
      });
  }
  
  private JTextPane[] generateTextPane(int arraySize, ArrayList arrayList) {
          JTextPane [] textPane = new JTextPane[arraySize];
          for(int i=0;i&lt;textPane.length;i++) {
              textPane[i]=new JTextPane();    
              textPane[i].setText((String) arrayList.get(i));
              System.out.println("length &gt;&gt; "+textPane.length);
          }
      return textPane;
  }
  
  public void addComponent(Container cont, 
           GridBagLayout gbl, 
           Component c,
           int x, int y,
           int width, int height,
           double weightx, double weighty) {
  
      GridBagConstraints gbc = new GridBagConstraints();
      gbc.fill = GridBagConstraints.HORIZONTAL;
      gbc.gridx = x; gbc.gridy = y;
      gbc.gridwidth = width; gbc.gridheight = height;
      gbc.weightx = weightx; gbc.weighty = weighty;
      gbc.anchor = GridBagConstraints.SOUTH;
      gbl.setConstraints( c, gbc );
      cont.add( c );
  }
  public void createFrame() {
      frame.add(menuBar, BorderLayout.NORTH);
      frame.add(scrollbar, BorderLayout.CENTER);
      frame.add(newItem, BorderLayout.SOUTH);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setSize(new Dimension(300,300));
      frame.setVisible(true);
  }
  public static void main(String [] args) {
      new RefreshPanel();
  }       
  }
  </code></pre>
  
  <p>I know that the grids are dynamically changing their size when I put a new element.
  Any ideas how can I solve my problem?</p>