3

Edit: In case the multiple tags are confusing, I'm working in Jython.

Here's my SSCCE:

from javax.swing import JFrame

window = JFrame('Test', 
                defaultCloseOperation = JFrame.EXIT_ON_CLOSE,
                size = (800, 600))
window.visible = True

The window opens, sits there for a few seconds, and then closes. So far the only solution I've found is to add while True: pass to the end, which would seem to indicate that the problem is that window 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 encountered previously.

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 properly.

Doing things the 'right way', creating the window on the EDT, gives exactly the same behaviour:

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)

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 invokeLater().

Update: Based on kingo's answer, I tried instantiating one of these:

class JFrameTest(JFrame):
    def addNotify(self):
        print 'In addNotify()'
        JFrame.addNotify(self)

    def removeNotify(self):
        print "In removeNotify()"
        JFrame.removeNotify(self)

"In addNotify()" is printed, but not "In removeNotify()", and the window behaves the same. Why would removeNotify() not be being called?

Also, I have tried doing window.setVisible(True) rather than window.visible = True, and that has no effect either.

Community
  • 1
  • 1
Cam Jackson
  • 11,860
  • 8
  • 45
  • 78
  • Wow, still running into problems with this? Hopefully someone will give the real answer this time. – agf Sep 16 '11 at 01:35
  • Haha yeah :/ It's a different problem this time, actually a much simpler one, but I suspect it might have the same root cause. Maybe I should log this as an issue at bugs.jython.org... – Cam Jackson Sep 16 '11 at 01:47
  • I've logged this [here](http://bugs.jython.org/issue1797), but if anyone here has a current solution/workaround that would be good too. – Cam Jackson Sep 16 '11 at 01:55
  • @Cam Jackson that isn't sscce, no idea what you are tried, not Java code, nor Swing relevant :-), – mKorbel Sep 16 '11 at 06:20
  • 1
    "In case the multiple tags are confusing, I'm working in Jython." – Cam Jackson Sep 16 '11 at 06:31
  • What version of jython/java are you running? With Jython 2.2.1 on Java 1.6.0_26 I can't reproduce this behavior. – job Sep 16 '11 at 14:24
  • @job I'm running Jython 2.5.2, Java 1.6.0_24. – Cam Jackson Sep 19 '11 at 00:26

1 Answers1

0

I suggest the following strategy to learn more about the problem:

sun.awt.AWTAutoShutdown is the class that prevents shutdown of the JVM if a native window peer is registered with the toolkit. Any component is registered when addNotify() is called on it. For a frame, this is done when you call setVisible(true).

The only way a peer can get unregistered is if somebody calls dispose() on the peer. The only place dispose() is called on a peer in the JRE is from Component#removeNotify().

You could override that method in your frame class and print a stack trace to see why that happens.

Ingo Kegel
  • 46,523
  • 10
  • 71
  • 102