title: Swing navigation - basic
body: <p>I just started working with Swing. I'm starting to get the hold of it.</p>
  
  <p>However I got a question regarding screen navigation in swing.</p>
  
  <p>This is how I have structured the app.</p>
  
  <ul>
  <li>public class MainFrame extends JFrame</li>
  <li>public class LoginPanel extends JPanel</li>
  <li>public class HomeScreenPanel extends JPanel</li>
  </ul>
  
  <p>in Mainframe I have the following code:</p>
  
  <pre><code>public class MainFrame extends JFrame {
  
      public MainFrame() {
          initUI();
      }
  
      public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
              public void run() {
                  MainFrame ex = new MainFrame();
                  ex.setVisible(true);
              }
          });
      }
  
      private void initUI(){
          InputScreenPanel inputScreenPanel = new InputScreenPanel();
          getContentPane().add(inputScreenPanel);
          Dimension dim = new Dimension(400, 300);
          setMinimumSize(dim);
      }
  }
  </code></pre>
  
  <p>In LoginPanel I initialize the screen and add a button and a textbox. I wrote the event handler for the button click event. I want to get the value from the textbox and redirect the user to HomeScreenPanel. How can I switch the panel when at the time this button click is executing the context is the LoginPanel and there I have no reference to the frame so I can switch the panels.</p>