title: Understanding GridBagConstraints gridx and gridy
body: <p>I am trying to use the gridx and gridy constraints to create an interface as shown below(ignore the Next button). The first two JLabel-JTextField pair works fine but I am not sure what gridx and gridy values do I set for the remaining 10 fields.</p>
  
  <p><img src="https://i.stack.imgur.com/Cg5Zc.jpg" alt="enter image description here"></p>
  
  <p>Code:</p>
  
  <pre><code>import java.awt.GridBagConstraints;
  import java.awt.GridBagLayout;
  
  import javax.swing.JLabel;
  import javax.swing.JPanel;
  import javax.swing.JTextField;
  
  public class InitialPanel extends JPanel {
  
      private static final long serialVersionUID = -3159520372145273536L;
  
      /**
       * @param gridBagLayout
       */
      public InitialPanel() {
  
          super();
  
          initComponents();
      }
  
      /**
       * For each component to be added to this container:
       * 
       * 1. Create the component 
       * 2. Set instance variables in the GridBagConstraints instance
       * 3. Add component to panel.
       * 
       */
      private void initComponents() {
  
          this.setLayout(new GridBagLayout());
  
          GridBagConstraints gridBagConstraints = new GridBagConstraints();
  
          // All mandatory fields.
  
          // 1.
  
          gridBagConstraints.gridx = 0;
  
          gridBagConstraints.gridy = 0;
  
          JLabel hwCodeLabel = new JLabel("HW CODE:");
  
          this.add(hwCodeLabel, gridBagConstraints);
  
  
          JTextField hwCodeTextField = new JTextField("");
  
          gridBagConstraints.gridx = 1;
  
          gridBagConstraints.gridy = 0;
  
          hwCodeTextField.setColumns(10);
  
          this.add(hwCodeTextField, gridBagConstraints);
  
  
          // 2.
  
          JLabel publisherIdLabel = new JLabel("PUBLISHER ID:");
  
          gridBagConstraints.gridx = 0;
  
          gridBagConstraints.gridy = 1;
  
          this.add(publisherIdLabel, gridBagConstraints);
  
  
  
          JTextField publisherIdTextField = new JTextField("");
  
          gridBagConstraints.gridx = 1;
  
          gridBagConstraints.gridy = 1;
  
          publisherIdTextField.setColumns(10);
  
          this.add(publisherIdTextField,gridBagConstraints);
  
  
  
          /*// 3.
          JLabel journalTitleLabel = new JLabel("JOURNAL TITLE:");
  
          gridBagConstraints.gridx = 0;
  
          gridBagConstraints.gridy = 2;
  
          this.add(journalTitleLabel,gridBagConstraints);
  
  
  
  
          JTextField journalTitleTextField = new JTextField();
  
          gridBagConstraints.gridx = 2;
  
          gridBagConstraints.gridy = 2;
  
          journalTitleTextField.setColumns(10);
  
          this.add(journalTitleTextField,gridBagConstraints);
  
  
          // 4.
  
  
          JLabel printISSNLabel = new JLabel("PRINT ISSN:");
  
          gridBagConstraints.gridx = 3;
  
          gridBagConstraints.gridy = 1;
  
          this.add(printISSNLabel,gridBagConstraints);
  
  
  
          JTextField printISSNTextField = new JTextField();
  
          gridBagConstraints.gridx = 3;
  
          gridBagConstraints.gridy = 2;
  
          journalTitleTextField.setColumns(10);
  
          this.add(printISSNTextField,gridBagConstraints);
  
  
          // 5.
  
          JLabel eISSNLabel = new JLabel("ELECTRONIC ISSN:");
  
          gridBagConstraints.gridx = 4;
  
          gridBagConstraints.gridy = 1;
  
          this.add(eISSNLabel,gridBagConstraints);
  
  
          JTextField eISSNTextField = new JTextField();
  
          gridBagConstraints.gridx = 4;
  
          gridBagConstraints.gridy = 2;
  
          journalTitleTextField.setColumns(10);
  
          this.add(eISSNTextField,gridBagConstraints);
  
  
          // 6.
  
          JLabel publisherNameLabel = new JLabel("PUBLISHER NAME:");
  
          gridBagConstraints.gridx = 5;
  
          gridBagConstraints.gridy = 1;
  
          this.add(publisherNameLabel,gridBagConstraints);
  
  
  
          JTextField publisherNameTextField = new JTextField();
  
          gridBagConstraints.gridx = 5;
  
          gridBagConstraints.gridy = 2;
  
          journalTitleTextField.setColumns(10);
  
          this.add(publisherNameTextField,gridBagConstraints);
  
  
  
          // 7.
  
          JLabel publisherLOCLabel = new JLabel("PUBLISHER LOCATION:");
  
          gridBagConstraints.gridx = 6;
  
          gridBagConstraints.gridy = 1;
  
          this.add(publisherLOCLabel,gridBagConstraints);
  
  
  
          JTextField publisherLOCTextField = new JTextField();
  
          gridBagConstraints.gridx = 6;
  
          gridBagConstraints.gridy = 2;
  
          journalTitleTextField.setColumns(10);
  
          this.add(publisherLOCTextField,gridBagConstraints);
  
  
  
          // 8.
  
          JLabel coverDateLabel = new JLabel("COVER DATE:");
  
          gridBagConstraints.gridx = 7;
  
          gridBagConstraints.gridy = 1;
  
          this.add(coverDateLabel,gridBagConstraints);
  
  
          JTextField coverDateTextField = new JTextField();
  
          gridBagConstraints.gridx = 7;
  
          gridBagConstraints.gridy = 2;
  
          journalTitleTextField.setColumns(10);
  
          this.add(coverDateTextField,gridBagConstraints);
  
  
          // 9.
  
          JLabel volumeLabel = new JLabel("VOLUME:");
  
          gridBagConstraints.gridx = 8;
  
          gridBagConstraints.gridy = 1;
  
          this.add(volumeLabel,gridBagConstraints);
  
  
  
          JTextField volumeTextField = new JTextField();
  
          gridBagConstraints.gridx = 8;
  
          gridBagConstraints.gridy = 2;
  
          journalTitleTextField.setColumns(10);
  
          this.add(volumeTextField,gridBagConstraints);
  
  
          // 10.
  
          JLabel issueLabel = new JLabel("ISSUE:");
  
          gridBagConstraints.gridx = 9;
  
          gridBagConstraints.gridy = 1;
  
          this.add(issueLabel,gridBagConstraints);
  
  
  
          JTextField issueTextField = new JTextField();
  
          gridBagConstraints.gridx = 9;
  
          gridBagConstraints.gridy = 2;
  
          journalTitleTextField.setColumns(10);
  
          this.add(issueTextField,gridBagConstraints);
  
  
          // 11.
  
          JLabel copyrightStatementLabel = new JLabel("COPYRIGHT STATEMENT:");
  
          gridBagConstraints.gridx = 10;
  
          gridBagConstraints.gridy = 1;
  
          this.add(copyrightStatementLabel,gridBagConstraints);
  
  
  
          JTextField copyrightStatementTextField = new JTextField();
  
          gridBagConstraints.gridx = 10;
  
          gridBagConstraints.gridy = 2;
  
          journalTitleTextField.setColumns(10);
  
          this.add(copyrightStatementTextField,gridBagConstraints);
  
  
          // 12.
  
          JLabel copyRightYearLabel = new JLabel("COPYRIGHT YEAR:");
  
          gridBagConstraints.gridx = 11;
  
          gridBagConstraints.gridy = 1;
  
          this.add(copyRightYearLabel,gridBagConstraints);
  
  
          JTextField copyRightYearTextField = new JTextField();
  
          gridBagConstraints.gridx = 11;
  
          gridBagConstraints.gridy = 2;
  
          journalTitleTextField.setColumns(10);
  
          this.add(copyRightYearTextField,gridBagConstraints);
  */
      }
  
  }
  </code></pre>