在 Swing 应用程序中制作 JavaFX 警报/对话框模式 [英] Making JavaFX Alerts/Dialogs Modal within Swing Application

查看:37
本文介绍了在 Swing 应用程序中制作 JavaFX 警报/对话框模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我们再次将完全使用 Swing 的现有 Java 应用程序转换为使用 JavaFX.但是,该应用程序不会完全使用 JavaFX.这似乎导致警报/对话框和模式出现一些问题.我们目前使用的是 Java 8u40.

So once again we are in the process of converting our existing Java application that was using entirely Swing to using JavaFX. However, the application will not be using JavaFX entirely. This seems to be causing some issues with Alerts/Dialogs and modality. We are currently using Java 8u40.

主要的应用程序基本上是在一个有菜单的 JFrame 中.主要内容窗格是 JDesktopPane,单击 MenuItem 会在 DeskopPane 中打开新的 JInternalFrames.目前,我们正在转换为 JavaFX 的屏幕基本上是 JInternalFrame 中的 JFXPanel.从 JFXPanel 打开的任何警报/对话框都是模态面板本身,但不是 JInternalFrame、DeskopPane、菜单等.

The main application is basically in a JFrame that has a Menu. The main content pane is JDesktopPane and clicking a MenuItem opens new JInternalFrames within the DeskopPane. Screens we are converting to JavaFX are basically JFXPanels within a JInternalFrame at the moment. Any Alerts/Dialogs that are opened from the JFXPanels are modal to the panel itself, but not to the JInternalFrame, DeskopPane, Menu, etc.

我在 DialogPane 文档中读到,他们计划在 JavaFX 的未来版本中引入一些轻量级对话框,甚至可能是 InternalFrames,所以也许我们只需要再等一段时间才能获得这个功能.但是,理想情况下,当打开一个新的警报/对话框时,它将是整个应用程序的模态.

I read in the DialogPane documentation that they are planning to introduce some lightweight dialogs and even possibly InternalFrames in future releases of JavaFX, so maybe we'll just have to wait it out a little longer for this functionality. But, ideally when opening a new Alert/Dialog it would be modal to the entire Application.

目前对模态对话框执行以下操作:

Currently doing the following for modal dialogs:

((Stage)getDialogPane().getScene().getWindow()).setAlwaysOnTop(true);

这使对话框始终显示在顶部,但是即使我们的主应用程序已最小化,对话框也仍位于其他应用程序的顶部.它也不会阻止框架中任何 Swing 组件的输入.

This makes the dialog always appear on top, however the dialog also remains on top of other applications even if our main application is minimized. It also does not block input to any Swing components in the frame.

推荐答案

Alert 显示和当 Alert 关闭时释放 JDialog.这种方法将模态扩展到整个应用程序,包括 Swing 部分.

You can use the following work-around which creates an invisible JDialog when the Alert is shown and disposes the JDialog when the Alert is closed. This approach extends the modality to the whole application, including the Swing part.

// create Alert
Alert alert = new Alert(AlertType.INFORMATION, "Hello");

// create invisible JDialog and "show" it
JDialog dialog = new JDialog();
dialog.setModal(true);
dialog.setUndecorated(true);
dialog.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
SwingUtilities.invokeLater(() -> dialog.setVisible(true));

// show Alert
alert.showAndWait();

// close JDialog after Alert is closed
dialog.dispose();

这篇关于在 Swing 应用程序中制作 JavaFX 警报/对话框模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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