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

查看:188
本文介绍了在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中打开新的JInternalFrame。我们转换为JavaFX的屏幕目前基本上是JInternalFrame中的JFXPanel。从JFXPanel打开的任何警报/对话框都是面板本身的模态,但不是JInternalFrame,DeskopPane,Menu等。

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.

推荐答案

我认为我完全理解你的问题。但这是我的猜测 - 您正在尝试从一些 JFXPanel 创建一个警告窗口,它将是模态的(即,在她关闭之前,用户将无法点击您的应用程序警报窗口)到整个应用程序,部分使用swing组件编写。

I don't think i understand your question completely. But here is my guess - You are trying to make an alert window from some JFXPanel that will be modal (i.e. user will not be able to click in your application until she closes that alert window) to your entire application which is written partially using swing components.

如果您的应用程序是用纯JavaFX编写的,那么您会做类似的事情(假设您在 JFXPanel

If your application would be written in purely JavaFX then you would do something like (Assuming you have created a button somewhere in your JFXPanel)

button.setOnAction(evt -> {
    Alert alert = new Alert(Alert.AlertType.INFORMATION);
    alert.initModality(Modality.APPLICATION_MODAL);

    // This will not work in your code
    alert.initOwner(button.getScene().getWindow());

    alert.show();
});

但是因为 initOwner 需要 javafx.stage.window 传递swing组件的对象在你的代码中不起作用。从Java 8u40开始,我认为没有正确的方法(即不是黑客)将 Alert 对象的所有权设置为swing组件。毫不奇怪,此处已经提出过这样的问题,并且在编写本文时尚未回答。

but since initOwner requires a javafx.stage.window object passing a swing component won't work in your code. As of Java 8u40 i don't think there is a right way(i.e. not hacks) to set ownership of Alert objects to swing component. Not surprisingly such questions has already been asked here and not answered as of writing this.

根据您的要求,您可以使用 JOptionPane.showMessageDialog 方法及其外观作为解决方法。

For your requirements you can use JOptionPane.showMessageDialog method and its look alike as workaround.

button.setOnAction(evt -> {
    JOptionPane.showMessageDialog(desktopPane,"My message");
});

默认情况下,这些对话框是模态的,因此无需任何工作。您可以从JavaFX组件的任何事件处理程序方法中调用它们。

These dialog boxes are modal by default so no work is necessary. You can call these from any event handler methods of JavaFX components.

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

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