title: cannot add dynamic jtextfields and save their values
body: <p>In previos my questions I asked similar questions to this. But in my previous projects I used GUI builder, so now I would like to add <code>JTextField</code> to the <code>Panel</code> dynamically without Builder. I don't why but for some reason I cannot execute this code:</p>
  
  <pre><code>public class Reference {
      JFrame frame = new JFrame();
      JPanel MainPanel = new JPanel();
      MainPanel main = new MainPanel();
      JPanel SubPanel = new JPanel();
      JButton addButton = new JButton();
      JButton saveButton = new JButton();
      private List&lt;JTextField&gt; listTf = new ArrayList&lt;JTextField&gt;();
      /**
       * @param args the command line arguments
       */
      public static void main(String[] args) {
         new Reference();
      }
  
      public Reference() {
          frame.add(main);
          frame.setLayout(new BorderLayout());
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.add(addButton, BorderLayout.EAST);
          frame.add(saveButton, BorderLayout.WEST);
          frame.pack();
          frame.setSize(500, 300); 
          frame.setVisible(true);
          main.setLayout(new BorderLayout()); 
          main.setBackground(Color.green);                 
          main.add(SubPanel);
          SubPanel.setBackground(Color.yellow);
  
          addButton.addActionListener(new ActionListener(){
                  public void actionPerformed(ActionEvent evt) {
                      main.add(new SubPanel());
                      main.revalidate();
                  }
              }); 
  
          saveButton.addActionListener(new ActionListener(){
  
                  public void actionPerformed(ActionEvent evt) {
                      for (int i = 0; i &lt; main.getComponentCount();) {
                  SubPanel panel = (SubPanel)main.getComponent(i);
                  JTextField firstName = panel.getFirstName();
                  String text = firstName.getText();
                  System.out.println( text );
    }}
        });
  
      }
  
      private class SubPanel extends JPanel {   
  
         JTextField firstName = new JTextField(15);
  
      public SubPanel() {
  
              this.setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
              this.add(firstName);
        listTf.add(firstName);
          }
      public JTextField getFirstName()
  {
      return firstName;
  }
      } 
  
  
    public class MainPanel extends JPanel
  {
      List&lt;SubPanel&gt; subPanels = new ArrayList&lt;SubPanel&gt;();
  
      public MainPanel()
      {
      }
  
      public void addSubPanel()
      {
          SubPanel panel = new SubPanel();
          add(panel);
          subPanels.add(panel);
      }
  
     public SubPanel getSubPanel(int index)
      {
          return subPanels.get(index);
      }
    }
  }
  </code></pre>
  
  <p>And by <code>saveButton</code> trying to get value of <code>JTextField</code>, but without success. In output I can see just <code>JFrame</code> with 2 <code>Buttons</code>, but <code>ActionListener</code> of <code>addButton</code> and <code>saveButton</code> is not active. I cannot understand where is wrong.</p>
  
  <p>Any help would be much appreciated.</p>