title: Why does my Swing window keep closing itself after a few seconds?
body: <p>Edit: In case the multiple tags are confusing, I'm working in Jython.</p>
  
  <p>Here's my <a href="http://sscce.org/" rel="nofollow noreferrer">SSCCE</a>:</p>
  
  <pre><code>from javax.swing import JFrame
  
  window = JFrame('Test', 
                  defaultCloseOperation = JFrame.EXIT_ON_CLOSE,
                  size = (800, 600))
  window.visible = True
  </code></pre>
  
  <p>The window opens, sits there for a few seconds, and then closes. So far the only solution I've found is to add <code>while True: pass</code> to the end, which would seem to indicate that the problem is that <code>window</code> is going out of scope, so it gets cleaned up and has to close. This may in fact be another symptom of the same problem I have <a href="https://stackoverflow.com/questions/6991878/why-is-my-file-getting-closed-if-i-dont-do-anything-with-it-for-a-while">encountered previously</a>.</p>
  
  <p>However, I don't think wasting cycles in an infinite loop is the best solution. I guess I could make it less of an issue by sleeping for a few seconds on each loop, but I would still like to solve this <em>properly</em>.</p>
  
  <p>Doing things the 'right way', creating the window on the EDT, gives exactly the same behaviour:</p>
  
  <pre><code>from javax.swing import JFrame, SwingUtilities
  from java.lang import Runnable
  
  class Foo(Runnable):
      def run(self):
          window = JFrame('Test', 
                          defaultCloseOperation = JFrame.EXIT_ON_CLOSE,
                          size = (800, 600))
          window.visible = True
  
  foo = Foo()
  SwingUtilities.invokeLater(foo)
  </code></pre>
  
  <p>In previous applications this hasn't been a problem, because I've needed an infinite loop for other tasks anyway (monitoring sockets etc). However, my current application is driven purely by user input, so I don't need/want anything after I <code>invokeLater()</code>.</p>
  
  <p><strong>Update:</strong> Based on kingo's answer, I tried instantiating one of these:</p>
  
  <pre><code>class JFrameTest(JFrame):
      def addNotify(self):
          print 'In addNotify()'
          JFrame.addNotify(self)
  
      def removeNotify(self):
          print "In removeNotify()"
          JFrame.removeNotify(self)
  </code></pre>
  
  <p>"In addNotify()" is printed, but not "In removeNotify()", and the window behaves the same. Why would <code>removeNotify()</code> not be being called?</p>
  
  <p>Also, I have tried doing <code>window.setVisible(True)</code> rather than <code>window.visible = True</code>, and that has no effect either.</p>