title: Getting Deep copy of customized JPanel in java
body: <p>I have an JPanel and it's a generic container, it could hold varied components and use varied Layout. </p>
  
  <p>My purpose is to make a deep copy of this JPanel, so that i could resize or change the backgroud color of the copied one without impact on the components in the original JPanel.</p>
  
  <p>First I tried to use Clone approach, but i found Clone just copy the top level, any components in the JPanel are shallow copied, that mean i just got references of origianl components.
  and then i saw some articles said it can be done by serialization the JPanel, but I get following error when I do so:</p>
  
  <p>java.io.NotSerializableException:sun.awt.windows.Win32OffScreenImage</p>
  
  <p>Anyone have any idea?</p>
  
  <p>Thanks,
  Sathish</p>
  
  <p>Update::</p>
  
  <p>This is the code to get deep copy.</p>
  
  <pre><code> static public Object deepCopy(Object oldObj) throws Exception {
      ObjectOutputStream oos = null;
      ObjectInputStream ois = null;
      try {
          ByteArrayOutputStream bos =
                  new ByteArrayOutputStream(); // A
          oos = new ObjectOutputStream(bos); // B
          oos.writeObject(oldObj);   // C
          oos.flush();               // D
          ByteArrayInputStream bin =
                  new ByteArrayInputStream(bos.toByteArray()); // E
          ois = new ObjectInputStream(bin);                  // F
          return ois.readObject(); // G
      } catch (Exception e) {
          System.out.println("Exception in ObjectCloner = " + e);
          throw (e);
      } finally {
          oos.close();
          ois.close();
      }
  }
  </code></pre>
  
  <p>I am passing JPanel that contains 2panels,jtable,2buttons.
  But now i get NullPointer Exception.
  Is there any other code to do this?</p>