不兼容的JVM不发送WindowClosing事件的解决方法 [英] Workaround for non-compliant JVM not sending WindowClosing events

查看:126
本文介绍了不兼容的JVM不发送WindowClosing事件的解决方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

各种OS X版本上的Apple JVM显然已被破坏,因为它们在应有的时候不生成WindowClosing事件(例如,如果您通过单击关闭按钮来关闭应用程序的主JFrame).

Apple JVM on various OS X version have apparently been broken in that they do not generate the WindowClosing event when they should (for example if you close an app's main JFrame using by clicking on the close button).

(在最新的Apple Java更新中,您可以设置一个属性来强制生成事件,但这不是我想要的)

我的问题很简单:当用户关闭应用程序时,我想显示一个提示".但是我(由于未生成任何事件)无法检测到用户关闭了窗口.

My problem is simple: I'd like to display a "tip" when the user closes the app. However I cannot (due to the fact that no event is generated) detected that the user closed the window.

所以我想我可以使用关机钩子:

So I thought I could use a shutdown hook:

Runtime.getRuntime().addShutdownHook(...)

但是显然从关闭钩子创建JFrame似乎有问题:就像调用关闭钩子后EDT是否已经消失一样.

However apparently creating a JFrame from a shutdown hook seems problematic: it's like if the EDT was already gone once the shutdown hook is called.

我尝试了几件事,但实际上似乎没有任何意义:就像我的"Tip" JFrame保持全灰色(尽管从关闭钩子以外的任何地方调用时,它都能正常工作)或程序立即退出.我尝试使用闩锁,然后从关闭钩子上等待闩锁,但是好像EDT不再存在了.

I tried several things and nothing really seems to makes sense: like my "Tip" JFrame staying all gray (despite it working fine when called from anywhere but the shutdown hook) or the program exiting immediately. I tried using a latch and waiting on the latch from the shutdown hook but it's as if the EDT wasn't there anymore.

我目前正在认真考虑生成第二个Java应用程序,只是为了将工具提示显示为一种解决方法,但我认为这有点过头了(但至少可以使用).

I'm currently seriously considering spawning a second Java app just to display the tooltip as a workaround but I think it's a bit overkill (but at least it would work).

有人曾经尝试过通过关闭钩子创建一个窗口并在EDT上调用东西吗?有没有需要注意的陷阱? (请记住,由于已知的长期存在的Apple VM错误,我无法可靠地捕获OS X上的窗口关闭事件.)

Did anyone ever try to create a window from a shutdown hook and invoke things on the EDT and are there any gotchas to be aware of? (remember that I cannot reliably catch the window closing events on OS X due to known very-longstanding Apple VM bugs).

推荐答案

如果窗口实际上正在关闭并且应用程序正在停止,则将调用JFrame.dispose()方法.覆盖它,然后在其中添加代码.

If the window is actually closing and the application is stopping then something is calling the JFrame.dispose() method. overwrite this, and add your code there.

否则,您可以添加一个守护程序线程,该线程在窗口侦听器上侦听已关闭的方法,该守护程序可以添加工具提示,然后处理窗口.您可以推迟处理,直到完成工具提示为止.

otherwise you can add a daemon thread that listens to the closed method on the window listener, the daemon can add the tooltip and then dispose of the window. you can delay the dispose until the tooltip is done.

我从未听说过此错误,但是由于苹果不发布自己的jdk,因此情况只会变得更好.

I've never heard of this bug, but things can only get better now that apple isnt releasing its own jdk's.

这篇关于不兼容的JVM不发送WindowClosing事件的解决方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆