title: Clear a JPanel from a JFrame
body: <p>I'm working on an assignment for class where we need to create a JComboBox and each option opens a new window where you can do whatever you want on those new windows.  Please keep in mind I'm very new to GUI and new to Java in general, in case my questions are dumb. </p>
  
  <p>I have a question and an issue...</p>
  
  <p>My question:<br>
  <strong>When the user selects "The Matrix" option a new window pops up with a quote and two buttons.  Right now I have two JPanels (panel and panel2) panel adds the quote to the NORTH position and then panel2 adds the two buttons to the CENTER position both using BorderLayout.  My question is am I doing this correctly...Could I use just panel to add the quote and the buttons or is it necessary to create separate panels for separate items being added to the JFrame? When I had them both added to the same panel the quote was not on the window when I ran the program.</strong></p>
  
  <pre><code>    panel.add(matrixQuote);
      newFrame.add(panel, BorderLayout.NORTH);
  </code></pre>
  
  <p>That's how I had it when it wasn't showing up ^^^</p>
  
  <p><strong>I GOT THE ISSUE WITH CLEARING THE JFRAME FIXED</strong><br>
  I am trying to add an ActionListener to the bluePill button and instead of opening another new window I thought I could clear everything from the existing window when the button is pressed and then display something new on said window.  The only info I could find on this is how I have it in the actionPerformed method below.  I'll post a snippet of what I'm talking about directly below and then all my code below that just in case.</p>
  
  <p>All my code...</p>
  
  <pre><code>public class MultiForm extends JFrame{
  
      private JComboBox menu;
      private JButton bluePill;
      private JButton redPill;
      private JLabel matrixQuote;
      private int matrixSelection;
      private JFrame newFrame;
      private JPanel panel;
      private JPanel panel2;
      private static String[] fileName = {"", "The Matrix", "Another Option"};
  
  public MultiForm() {
      super("Multi Form Program");        
      setLayout(new FlowLayout());
      menu = new JComboBox(fileName);
      add(menu);
  
      TheHandler handler = new TheHandler();
      menu.addItemListener(handler);  
  
  }
  
  public void matrixPanel() {
  
      TheHandler handler = new TheHandler();
      //Create a new window when "The Matrix" is clicked in the JCB
      newFrame = new JFrame();
      panel = new JPanel();
      panel2 = new JPanel();
  
      newFrame.setLayout(new FlowLayout());
      newFrame.setSize(500, 300);
      newFrame.setDefaultCloseOperation(newFrame.EXIT_ON_CLOSE);      
  
      matrixQuote = new JLabel("&lt;html&gt;After this, there is no turning back. "
              + "&lt;br&gt;You take the blue pill—the story ends, you wake up "
              + "&lt;br&gt;in your bed and believe whatever you want to believe."
              + "&lt;br&gt;You take the red pill—you stay in Wonderland, and I show"
              + "&lt;br&gt;you how deep the rabbit hole goes. Remember: all I'm "
              + "&lt;br&gt;offering is the truth. Nothing more.&lt;/html&gt;");
  
      panel2.add(matrixQuote);
      newFrame.add(panel2, BorderLayout.NORTH);
  
      //Blue pill button and picture.
  
      Icon bp = new ImageIcon(getClass().getResource("Blue Pill.png"));
      bluePill = new JButton("Blue Pill", bp);
      panel2.add(bluePill);   
      bluePill.addActionListener(handler);
  
      //Red pill button and picture
      Icon rp = new ImageIcon(getClass().getResource("Red Pill.png"));
      redPill = new JButton("Red Pill", rp);
      panel2.add(redPill);
  
      newFrame.add(panel2, BorderLayout.CENTER);      
      newFrame.setVisible(true);
  }
  
  private class TheHandler implements ItemListener, ActionListener{
  
      public void itemStateChanged(ItemEvent IE) {
          //listen for an item to be selected.
          if(IE.getStateChange() == ItemEvent.SELECTED) {
              Object selection = menu.getSelectedItem();
  
              if("The Matrix".equals(selection)) {
                  matrixPanel();
              }
              else if("Another Option".equals(selection)) {   
              }
          }   
      }
  
      public void actionPerformed(ActionEvent AE) {
          if(AE.getSource() == bluePill) {
              newFrame.remove(panel);         
              newFrame.remove(panel2);
              newFrame.repaint();
          }
      }   
  }
  
  //MAIN
  public static void main(String[] args) {
      MultiForm go = new MultiForm();
      go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      go.setSize(400, 200);
      go.setVisible(true);
  }
  }
  </code></pre>