JDialog:如何禁用模态对话框的 ESC 键?

发布于 2024-09-28 17:19:59 字数 277 浏览 0 评论 0原文

所以有一个框架(主应用程序)。从这里,我打开一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

野心澎湃 2024-10-05 17:19:59

但是,用户可以随时点击 ESC 键,并且调用我的 onCanceled()

这听起来像是添加到应用程序中的自定义代码,因为大多数 LAF 默认情况下不实现 Escape 键。所以我会删除自定义代码。

但是,如果您的 LAF 存在这种默认行为,那么拦截 Escape 键的正确方法是使用 按键绑定。本教程展示了如何覆盖/删除绑定。

However, the user can at any time tap the ESC key and my onCanceled() is called

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.

雨轻弹 2024-10-05 17:19:59

您必须忽略 ESC 键的敲击。您可以通过侦听对话框中的关键事件来完成此操作,如下所示(假设变量 jDialog 是您的对话框对象)。

jDialog.addKeyListener(new KeyListener() {
    @Override
    public void keyPressed(KeyEvent e) {
        // Catch ESC key stroke.
        if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
            // TODO ignore or warn user here.
            // or call e.consume();
        }
    }

    // Other overriden methods here.
});

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

jDialog.addKeyListener(new KeyListener() {
    @Override
    public void keyPressed(KeyEvent e) {
        // Catch ESC key stroke.
        if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
            // TODO ignore or warn user here.
            // or call e.consume();
        }
    }

    // Other overriden methods here.
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文