title: How to set a Frame to display a new Panel in swing?
body: <p>I'm obviously missing something here.</p>
  
  <p>How can I change a swing Frame to display an entirely new Panel?</p>
  
  <p>As in either:
  1.  display an entirely new panel with new content
                    or
  2. display the same panel cleared with my new added</p>
  
  <p>I have displayed my new panel in an alternative program and it creates and displays correctly.</p>
  
  <p>However when the panel with my buttons have previously been in the frame it never displays my new panel.</p>
  
  <p>I call contentPane.revalidate(); a few times in my code to update my buttons which updates them flawlessly the problem only occurs when I'm trying to remove the old buttons and add new.</p>
  
  <p>After having added my new panels to the contentPane I have tried:</p>
  
  <pre><code>contentPane.validate();
  contentPane.revalidate();
  contentPane.removeAll();        
  contentPane.repaint();
  setContentPane(contentPane);
  </code></pre>
  
  <p>but it never changes.</p>
  
  <pre><code> @Override
  public void actionPerformed(ActionEvent e) 
  {
      String buttonPressed = e.getActionCommand();
      int pos = Integer.valueOf(buttonPressed);
  
      if (Control.model.cardsRemaining == 0) 
      {
          contentPane.removeAll();
          //contentPane.validate();
          //contentPane = new JPanel();
          //contentPane.add(createSuccess());
          contentPane = createSuccess();
          contentPane.revalidate();
          contentPane.repaint();      
  
          System.out.println("entered success");
      }
  
      else 
      {
          System.out.println("Cards left: " + Control.model.cardsRemaining);
          action = Control.model.ReceiveCardsTurned(pos);
  
          keypadArray[pos].setIcon(myIcons[pos]);
          currentTime.setText("" + Control.model.time);
          currentScore.setText("" + Control.model.score);
  
          System.out.println("this card: " + pos + "last card: "
                  + Control.model.lastCard);
  
          if (action == "unturn") 
          {
              try 
              {
                  Thread.sleep(1000);
              }
  
              catch (InterruptedException e1) 
              {
                  e1.printStackTrace();
              }
  
              contentPane.revalidate();
              keypadArray[pos].setIcon(back);
              keypadArray[Control.model.lastCard].setIcon(back);
          }
  
          //System.out.println(action);
      }
  }
  
  
  
  private JPanel createSuccess() 
  {
      //final 
      JPanel Success = new JPanel();
      JLabel image = new JLabel(success);
      Success.add(image);
  
      return Success; 
  }
  </code></pre>
  
  <p>Also this is further up in my code:</p>
  
  <pre><code>ImageIcon success = new ImageIcon("icons/success.png");
  </code></pre>
  
  <p>And:</p>
  
  <pre><code>JPanel contentPane = new JPanel();
  </code></pre>
  
  <p>just tried:</p>
  
  <pre><code>contentPane = createSuccess(); 
  frame.setContentPane(contentPane);  
  frame.pack();                
  </code></pre>
  
  <p>line 268 is: frame.setContentPane(contentPane); </p>
  
  <pre><code>Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
  at pairs.GUI.actionPerformed(GUI.java:268)
  at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
  at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
  at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
  at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
  at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
  at java.awt.Component.processMouseEvent(Unknown Source)
  at javax.swing.JComponent.processMouseEvent(Unknown Source)
  at java.awt.Component.processEvent(Unknown Source)
  at java.awt.Container.processEvent(Unknown Source)
  at java.awt.Component.dispatchEventImpl(Unknown Source)
  at java.awt.Container.dispatchEventImpl(Unknown Source)
  at java.awt.Component.dispatchEvent(Unknown Source)
  at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
  at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
  at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
  at java.awt.Container.dispatchEventImpl(Unknown Source)
  at java.awt.Window.dispatchEventImpl(Unknown Source)
  at java.awt.Component.dispatchEvent(Unknown Source)
  at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
  at java.awt.EventQueue.access$200(Unknown Source)
  at java.awt.EventQueue$3.run(Unknown Source)
  at java.awt.EventQueue$3.run(Unknown Source)
  at java.security.AccessController.doPrivileged(Native Method)
  at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
  at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
  at java.awt.EventQueue$4.run(Unknown Source)
  at java.awt.EventQueue$4.run(Unknown Source)
  at java.security.AccessController.doPrivileged(Native Method)
  at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
  at java.awt.EventQueue.dispatchEvent(Unknown Source)
  at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
  at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
  at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
  at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
  at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
  at java.awt.EventDispatchThread.run(Unknown Source)
  </code></pre>
  
  <p>Thank you my friend that worked perfectly:</p>
  
  <pre><code>contentPane.removeAll();
  contentPane.add(createSuccess());
  setContentPane(contentPane);
  </code></pre>