Why does my Swing window keep closing after a few seconds?

Edit: If multiple tags are confusing, I am working in Jython.

Here is 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 have found is to add while True: pass to the end, which seems to indicate that the problem is that the window is out of scope, so it clears and closes. In fact, this may be another symptom of the same problem that I encountered earlier .

However, I do not think that wasting cycles in an infinite loop is the best solution. I probably could have done this less problem by sleeping for a few seconds on each cycle, but I still would like to solve it correctly.

Doing the β€œright path”, creating a window on the EDT, gives exactly the same behavior:

 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 was not a problem, because in any case, I need an infinite loop for other tasks (socket monitoring, etc.). However, my current application is driven entirely by user input, so I don't need / need anything after I invokeLater() .

Update:. Based on the answer to the royal language, I tried to create an instance of one of them:

 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 won't removeNotify() be called?

Also, I tried doing window.setVisible(True) , not window.visible = True , and this also has no effect.

+3
java python user-interface jython swing
source share
1 answer

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

sun.awt.AWTAutoShutdown is a class that prevents the JVM from stopping if its own tool file is registered in the toolbox. 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 be unregistered is if someone calls dispose() on the peer. The only place dispose() is called on the peer in the JRE from Component#removeNotify() .

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

0
source share

All Articles