title: How to listen for close in a JPanel
body: <p>I am working with some strange legacy code. They have a custom object which implements a JPanel. This <code>JPanel</code> object is a secondary popup screen within the main application. The issue I'm having is to detect when the secondary popup screen is closed. </p>
  
  <p>I tried to implement a <code>WindowListener</code> for the class, but when I try to add it, there is no <code>JFrame</code> associated with this object. I am assuming this is because they are using a custom object and it is an embedded popup screen.</p>
  
  <p>I tried to retrieve a <code>JFrame</code> using:     </p>
  
  <pre><code>JFrame parentFrame = (JFrame) SwingUtilities.getWindowAncestor(this);
  </code></pre>
  
  <p>which fails on a <code>NullPointerException</code>. I have no idea why it's so difficult to detect the right hand corner "x" close button on this page! I should mention that they were able to add Mouse and Key Listeners to the table which is embedded within the <code>JPanel</code>. But the outside listener for the entire window is causing me troubles.</p>
  
  <p>(Please bear with me, this is my first stackoverflow post and I am new to Swing.) </p>
  
  <p>Thanks so very much!!</p>