Creating JavaFX Modal Alerts and Dialogs in a Swing Application

So, once again, we are in the process of converting our existing Java application, which makes full use of Swing to use JavaFX. However, the application will not fully use JavaFX. This seems to cause some problems with Alerts / Dialogs and modality. We are currently using Java 8u40.

The main application mainly consists of a JFrame with a menu. The main content area is JDesktopPane, and clicking on MenuItem opens up new JInternalFrames inside DeskopPane. The screens we convert to JavaFX are basically JFXPanels inside a JInternalFrame. Any warnings / dialogs opened from JFXPanels are modal for the panel itself, but not for JInternalFrame, DeskopPane, Menu, etc.

In the DialogPane documentation, I read that in future releases of JavaFX they plan to introduce some lightweight dialogs and even, possibly, internal frames, so we may have to wait a bit for this function. But ideally, when you open a new Alert / Dialog, it will be modal for the entire Application.

EDIT: Currently, for modal dialogs, the following is done:

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

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

+5
source share
1 answer

I don’t think I fully understand your question. But here is my hunch. You are trying to create a warning window from some JFXPanel that will be modal (i.e. the user will not be able to click your application until it closes this warning window) for your entire application, which is written partially using rotation components.

If your application is written in pure JavaFX, you will do something like (assuming you 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(); }); 

but since initOwner requires the initOwner object that passes the swing component to not work in your code. As for Java 8u40, I don’t think there is a proper way (i.e. Do not hack) to establish ownership of the Alert objects on the swing component. It is not surprising that such questions have already been asked here and did not answer this by writing this.

For your requirements, you can use the JOptionPane.showMessageDialog method and its appearance as a workaround.

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

These dialog boxes are modal by default, so no work is required. You can call them from any JavaFX component event handler methods.

+2
source

Source: https://habr.com/ru/post/1211562/


All Articles