Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making JavaFX Alerts/Dialogs Modal within Swing Application

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.

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.

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.

EDIT: Currently doing the following for modal dialogs:

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

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.

like image 420
Tommo Avatar asked Jan 21 '15 01:01

Tommo


People also ask

How to give Alert in JavaFX?

AlertType a): Creates a new alert with a specified alert type. Alert(Alert. AlertType a, String c, ButtonType… b): Creates a new alert with a specified alert type, content and button type.

Are all dialogs created by JOptionPane modal?

All the dialogs that JOptionPane provides are modal. To create a non-modal dialog, you must use the JDialog class directly. class. It adds to Dialog a root pane and support for a default close operation.

What is JDialog modality?

JDialog(Dialog owner, boolean modal) Creates a dialog box with the specified Dialog owner and modality. JDialog(Dialog owner, String title) Creates a modeless dialog box with the specified Dialog owner and title.

What is Dialog in JavaFX?

A Dialog in JavaFX wraps a DialogPane and provides the necessary API to present it to end users. In JavaFX 8u40, this essentially means that the DialogPane is shown to users inside a Stage , but future releases may offer alternative options (such as 'lightweight' or 'internal' dialogs).


1 Answers

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();
like image 191
user7291698 Avatar answered Nov 09 '22 16:11

user7291698