JDialog:如何禁用模态对话框的 ESC 键?
所以有一个框架(主应用程序)。从这里,我打开一个 Modal JDialog 并启动一个后台线程,同时在表中显示进度(日志条目)。此过程至关重要,不应该不能可停止/隐藏/可关闭,因此对话框的关闭按钮在一切完成之前处于停用状态。但是,用户可以随时点击 ESC 键,然后调用 onCanceled(),从而调用 this.dispose()。
编辑: 我继承了这个项目并监督了继承的兔子洞有多深,因此已经监督了 ESC 的处理,然后是 e.consume(),这就是我的解决方案不起作用的原因!
So there's a frame (main app). From here, I open a Modal JDialog and start a background thread working whilst displaying progress (log entries) in a table. This process is critical and should not be stoppable/hideable/closeable, thus why the dialog's close button is de-activated until everything's finished. However, the user can at any time tap the ESC key and my onCanceled() is called, thus calling this.dispose().
EDIT:
I inherited this project and oversaw how deep the rabbit hole of inheritance went, thus overseeing handling of ESC already, followed by e.consume() which is why my solutions weren't working!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这听起来像是添加到应用程序中的自定义代码,因为大多数 LAF 默认情况下不实现 Escape 键。所以我会删除自定义代码。
但是,如果您的 LAF 存在这种默认行为,那么拦截 Escape 键的正确方法是使用 按键绑定。本教程展示了如何覆盖/删除绑定。
This sounds like custom code added to the APP since most LAF's don't implement the Escape key by default. So I would remove the custom code.
However,if this default behaviour for your LAF then the proper way to intercept the Escape key is to use Key Bindings. The tutorial shows how to override/remove a binding.
您必须忽略 ESC 键的敲击。您可以通过侦听对话框中的关键事件来完成此操作,如下所示(假设变量 jDialog 是您的对话框对象)。
You must ignore strokes from ESC key. You can do this by listening key events from your dialog as the following (Suppose variable jDialog is your dialog object).