I have an application built on Swing integrated with JavaFX. Swing's JFrame is the top-level window, with JFXPanel housing different JavaFX controls. Now, I am also integrating JavaFX's new Alert API, and currently having difficulty in setting Alert's ownership when shown. That is, I would like to make JFrame as the owner of Alert.
JFXPanel fxPanel = new JFXPanel();
Platform.runLater(() -> {
Button button = new Button("Alert");
button.setOnAction(evt -> {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setHeaderText("An Alert");
alert.setContentText("Alerting");
alert.initModality(Modality.APPLICATION_MODAL);
alert.initOwner(button.getScene().getWindow());
alert.initStyle(StageStyle.UTILITY);
alert.show();
});
BorderPane borderPane = new BorderPane();
borderPane.setCenter(button);
Scene scene = new Scene(borderPane, 300, 300);
SwingUtilities.invokeLater(() -> {
fxPanel.setScene(scene);
JFrame frame = new JFrame("App");
frame.add(fxPanel);
frame.setSize(300, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
});
});
I understand that when a window reference is taken in this code alert.initOwner(button.getScene().getWindow());
, com.sun.javafx.stage.EmbeddedWindow
object is returned. I know that it would be impossible to take JFrame as the window owner of Alert. But, is there a hack to make this happen?