5

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?

Warren Nocos
  • 1,222
  • 1
  • 14
  • 29
  • Does this answer your question? [Making JavaFX Alerts/Dialogs Modal within Swing Application](https://stackoverflow.com/questions/28058142/making-javafx-alerts-dialogs-modal-within-swing-application) – user7291698 Nov 06 '19 at 12:49

1 Answers1

-3

Look here: http://docs.oracle.com/javase/8/javafx/interoperability-tutorial/swing-fx-interoperability.htm

It says something like this ->

JFrame frame = new JFrame("Swing and JavaFX");
final JFXPanel fxPanel = new JFXPanel();
frame.add(fxPanel);
Juce
  • 341
  • 1
  • 8