title: Opening a new JFrame from a current JFrame
body: <p>I am creating a Java application where I am going to have a button. When that button is pressed I want a form with fields like <code>username</code>, <code>password</code> and various others to be entered there. In that form there is the <code>Save</code> button which when pressed saves everything to a file and switches back to the first JFrame.</p>
  
  <p>This is how I do it currently. This is the button code for the first JFrame:</p>
  
  <pre><code>public class FirstJFrame extends JFrame {    
      private void newJFrameActionPerformed(java.awt.event.ActionEvent evt) {                                          
          SecondJFrame pan = new SecondJFrame();
          this.setVisible(false);
          pan.setVisible(true);
      }
  }
  </code></pre>
  
  <p>and this is the code for the <code>Save</code> button on the second JFrame:</p>
  
  <pre><code>public class SecondJFrame extends javax.swing.JFrame {
      private void goBackActionPerformed(java.awt.event.ActionEvent evt) {                                       
          SecondJFrame pan = new SecondJFrame();
          this.setVisible(false);
          pan.setVisible(true);
      }
  }
  </code></pre>
  
  <p>This works well enough for me but I have a feeling that I am not doing this right at all and I might create a memory leak since I am creating a new object each time. Is what I am doing correct or is there a better way?</p>