title: Is it possible to make the elements inside a JPanel with GridBagLayout to start from the top left?
body: <p>I have a Swing Form that contains a JScrollPane(<code>activityScrollPane</code>) for a JPanel(<code>activityPanel</code>). The panel contains a JTextField and a JButton (that is used to add more fields to the Panel). Now the problem is that the elements start from the center of the panel as in the image below (with the borders marking the <code>activityScrollPane</code> boundary)</p>
  
  <p><img src="https://i.stack.imgur.com/TY6yl.png" alt="enter image description here"></p>
  
  <p>Following is the code I am currently using to make the scroll pane and associated components.</p>
  
  <pre><code> //part of the code for creating the ScrollPane
  
          final JPanel activityPanel = new JPanel(new GridBagLayout());
          gbc.gridx=0;
          gbc.gridy=0;
          JScrollPane activityScrollPane = new JScrollPane(activityPanel); 
          //adding activity fields
          activityFields = new ArrayList&lt;JTextField&gt;();
          fieldIndex = 0;
          activityFields.add(new JTextField(30));
  
          final GridBagConstraints activityGBC = new GridBagConstraints();
          activityGBC.gridx=0;
          activityGBC.gridy=0;
          activityGBC.anchor = GridBagConstraints.FIRST_LINE_START;
          activityPanel.add(activityFields.get(fieldIndex),activityGBC);
  
          fieldIndex++;
          JButton btn_more = (new JButton("more"));
          activityGBC.gridx=1;
          activityPanel.add(btn_more,activityGBC);
  </code></pre>
  
  <p>How can I make the JTextField and the JButton or for that matter any component to appear on the top left corner of the JScrollPane. I have already tried using</p>
  
  <pre><code>activityConstraints.anchor = GridBagConstraints.NORTHWEST;
  </code></pre>
  
  <p>as pointed in the SO <a href="https://stackoverflow.com/questions/6364280/starting-gridbaglayout-from-top-left-corner-in-java-swing">post</a>, but it does not at all seem to work.</p>