title: Duplicating a JPanel - (Copy JPanel's static image to different JPanel)
body: <p>I'm pretty new to swing and I would like to receive some help as Im stuck with a task.</p>
  
  <p><strong>Current state:</strong></p>
  
  <p>Im having a nice <code>JFrame</code> object (guiFrame) which is having two <code>JPanel</code> on it (tabsPanel and cardPanel)(one is a simple <code>JPanel</code> with buttons, the other has <code>CardLayout</code> which is switched by the tabsPanel buttons). </p>
  
  <p><strong>Problem:</strong></p>
  
  <p>The task is that if I press the button "Show" on tabsPanel I need to send the cardPanel to a different window (ShowFrame) as a static "image", while on the previous window the program is still running and nice. So basicly Im trying to copy / clone the cardPanel.</p>
  
  <p><strong>What I have tried:</strong></p>
  
  <ul>
  <li><p>I have tried to simply </p>
  
  <pre><code>JPanel jPanelShow = cardPanel; 
  show.add(jPanelShow);
  </code></pre>
  
  <p>Of course not working because the reference number is being copied and if I run the program, the cardPanel "disappears".</p></li>
  <li><p>I have tried to use <code>clone()</code>
  For this its almost working but I'm getting some weird <code>NullPointerException</code> which isn't caused by my code.</p></li>
  </ul>
  
  <p><strong>Current codes (cloning try):</strong></p>
  
  <p>CardPanel.java</p>
  
  <pre><code>/**
   * This is basicly a JPanel, just with a clone() implemented
   */
  package javaapplication5;
  
  import javax.swing.JPanel;
  import java.util.Stack;
  
  public class CardPanel extends JPanel implements Cloneable {
  
      public CardPanel() {
          super();
      }
  
      @Override
      public CardPanel clone() throws NullPointerException {
          /* Creating return object */
          final CardPanel copy;
  
          try {
  
              /* Cloning */
              copy = (CardPanel) super.clone();
          } catch (CloneNotSupportedException e) {
  
              /* Exception (should not happen though) */
              e.printStackTrace();
              return null;
          }
          return copy;
      }
  }
  </code></pre>
  
  <p>CardLayoutExample.java</p>
  
  <pre><code>package javaapplication5;
  
  import java.awt.BorderLayout;
  import java.awt.CardLayout;
  import java.awt.Color;
  import java.awt.Container;
  import java.awt.EventQueue;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import javax.swing.BorderFactory;
  import javax.swing.JButton;
  import javax.swing.JFrame;
  import javax.swing.JPanel;
  import javax.swing.border.Border;
  
  public class CardLayoutExample {
  
  
      JFrame guiFrame;
      CardLayout cards;
      CardPanel cardPanel;
      private int showFrameNotShownYet = 1;
      public ShowFrame show = new ShowFrame();
  
      public static void main(String[] args) {
  
           /* Random things for Swing */
           EventQueue.invokeLater(new Runnable()
           {            
              @Override
               public void run()
               {     
                   new CardLayoutExample();         
               }
           });
  
      }
  
      public CardLayoutExample()
      { 
          /* Creating the main JFrame */
          guiFrame = new JFrame();
  
          /* Making sure the program exits when the frame closes */
          guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          guiFrame.setTitle("CardLayout Example");
          guiFrame.setSize(400,300);
  
          /* This will center the JFrame in the middle of the screen */
          guiFrame.setLocationRelativeTo(null);
          guiFrame.setLayout(new BorderLayout());
  
          /* Border for JPanel separation */
          Border outline = BorderFactory.createLineBorder(Color.black);
  
          /* Creating JButton1 for tabsPanel */
          JButton switchCards1 = new JButton("1");
          switchCards1.setActionCommand("1");
          switchCards1.addActionListener(new ActionListener()
          {
              @Override
              public void actionPerformed(ActionEvent event)
              {
                  cards.show(cardPanel, "TestContent");     
              }
          });
  
          /* Creating JButton2 for tabsPanel */
          JButton switchCards2 = new JButton("2");
          switchCards2.setActionCommand("2");
          switchCards2.addActionListener(new ActionListener()
          {
              @Override
              public void actionPerformed(ActionEvent event)
              {
                  cards.show(cardPanel, "TestContent1");      
              }
          });
  
          /* Creating JButton3 for tabsPanel */
          JButton switchCards3 = new JButton("3");
          switchCards3.setActionCommand("3");
          switchCards3.addActionListener(new ActionListener()
          {
              @Override
              public void actionPerformed(ActionEvent event)
              {
                  cards.show(cardPanel, "TestContent2");    
              }
          });
  
          JButton switchCards4 = new JButton("Show");
          switchCards4.setActionCommand("Show");
          switchCards4.addActionListener(new ActionListener()
          {
              @Override
              public void actionPerformed(ActionEvent event)
              {
  
                  /* If there is no ShowFrame yet */
                  if(showFrameNotShownYet == 1){
  
                      show.add((JPanel)cardPanel.clone());
                      show.setVisible(true);
                      showFrameNotShownYet = 0; 
                  } 
  
                  /* If there is a ShowFrame already */
                  else {
                      show.setVisible(false);
                      showFrameNotShownYet = 1;
                      guiFrame.repaint();
                  }   
              }
          });
  
          JButton switchCards5 = new JButton("Refresh");
          switchCards5.setActionCommand("Refresh");
          switchCards5.addActionListener(new ActionListener()
          {
              @Override
              public void actionPerformed(ActionEvent event)
              {                    
                      show.setVisible(false);
                      show.add((JPanel)cardPanel.clone());
                      show.setVisible(true);
                      showFrameNotShownYet = 0;
              }
          });
  
          /* Creating JPanel for buttons */
          JPanel tabsPanel = new JPanel();
          tabsPanel.setBorder(outline);
          tabsPanel.add(switchCards1);
          tabsPanel.add(switchCards2);
          tabsPanel.add(switchCards3);
          tabsPanel.add(switchCards4);
          tabsPanel.add(switchCards5);
  
          /* Creating JPanel for CardLayout */
          cards = new CardLayout();
          cardPanel = new CardPanel();
          cardPanel.setLayout(cards);
          cards.show(cardPanel, "TestContent");
  
          /* Adding 1st card */
          JPanel firstCard = new TestContent();
          cardPanel.add(firstCard, "TestContent");
  
          /* Adding 2nd card */
          JPanel secondCard = new TestContent1();
          cardPanel.add(secondCard, "TestContent1");
  
          /* Adding 3rd card */
          JPanel thirdCard = new TestContent2();
          cardPanel.add(thirdCard, "TestContent2");
  
          /* Filling up JFrame with stuff */
          guiFrame.add(tabsPanel,BorderLayout.NORTH);
          guiFrame.add(cardPanel,BorderLayout.CENTER);
          guiFrame.setVisible(true);
      }
  
  }
  </code></pre>
  
  <p>TestContent, TestContent1 and TestContent2 are simple JPanels with random stuff generated by SwingGUI, just as ShowFrame is empty JFrame. But if needed I will paste in those codes too.</p>