2

Display a swings JDialog containing a JRViewer for Jasper Report, from within a javafx application menu item click. BUT the JDialog is not MODAL even after setModal(true) as it is not owned by javafx stage. How to make a javafx stage the owner of a JDialog? Alternatively how to display a Jasper report inside a javafx stage, scene?

Charles Menguy
  • 40,830
  • 17
  • 95
  • 117
user1693781
  • 21
  • 1
  • 1
  • 2

6 Answers6

3

Don't use a JDialog. In JavaFX you use Stages. Put your Jasper report inside a Scene, put the Scene in a Stage. I think you can't put Swing components inside JavaFX components, so you must check this link: Integrating JavaFX into Swing Applications

Code to make a JavaFX dialog:

            Stage stage = new Stage();
            stage.setTitle("Title");
            stage.setResizable(false);
            stage.initModality(Modality.APPLICATION_MODAL);
            stage.initOwner(primaryStage);
            stage.initStyle(StageStyle.UTILITY);

            (...)

            Scene scene = new Scene(...);
            stage.setScene(scene);

In the code above the Stage will behave exactly like a dialog.

Also check this:

How to create a JavaFX dialog?

Hello World, JavaFX Style

Community
  • 1
  • 1
ceklock
  • 6,143
  • 10
  • 56
  • 78
3

Back to the first (base) question: It's not possible to make a Stage the owner of a JDialog. But, after setting the content of the SwingNode

swingNode.setContent(xxx);

you can get the parent of xxx (may be the parent of the parent of xxx too...) until you find a (AWT-) Frame or (AWT-) Dialog. In the case of SwingNode it's a JLightweightFrame. This object you can use as the owner to create your JDialog.

But, I think there is a bug in the JavaFX SwingNode mechanism. The JDialog appears functionally modal, if you set it modal (setModal(true)), but it may appear behind the main (JavaFX) window. It's still possible to move the main window, to bring it in front and so on, what should not be possible if there is a modal dialog. But this is another topic.

Floern
  • 33,559
  • 24
  • 104
  • 119
Steffen
  • 31
  • 5
1

To view a Jasper Report in javafx ( jdk 1.8> ) stage

JasperPrint jp = JasperFillManager.fillReport(reportsource, params,getCon());

                SwingNode swingNode = new SwingNode();
                swingNode.setContent(new JRViewer(jp));

                AnchorPane anchorPane = new AnchorPane();

                AnchorPane.setTopAnchor(swingNode,0.0);
                AnchorPane.setBottomAnchor(swingNode,0.0);
                AnchorPane.setLeftAnchor(swingNode,0.0);
                AnchorPane.setRightAnchor(swingNode,0.0);

                anchorPane.getChildren().add(swingNode);
                Scene scene = new Scene(anchorPane);
                Stage stage = new Stage();
                        stage.setHeight(550);
                        stage.setWidth(600);
                        stage.setAlwaysOnTop(true);
                        stage.setScene(scene);
                        stage.showAndWait();
RAINA
  • 802
  • 11
  • 22
0

Alternatively how to display a Jasper report inside a javafx stage, scene?

Output HTML from your Jasper report and load the report in a JavaFX WebView.

Make a javafx stage the owner of a JDialog?

I don't think this is possible. What should work instead is to make your application a Swing application and host your JavaFX content inside a JFXPanel inside a Swing JFrame. Then you have no Stage and the owner of the JDialog can be the JFrame hosting the JFXPanel.

Community
  • 1
  • 1
jewelsea
  • 150,031
  • 14
  • 366
  • 406
0

now it's possible to do that
what you need now is install jdk 8
because swing content in javafx is introduced in jdk8

@FXML
private SwingNode swingNode;
...
JasperPrint jasperPrint = JasperFillManager.fillReport(FileName, hash, connect);
swingNode.setContent(new JRViewer(jasperPrint));

oracle just add tutorial for this too
for the next you can follow this link
oracle tutorial embed swing in javaFX

buncis
  • 2,148
  • 1
  • 23
  • 25
0

I have written a JasperReports print preview stage class in JavaFX 8. It is a pure JavaFX code and there is no need for use of SwingNode class.

In comparison to the JRViewer it does not support links and parts, but print preview window does not need it anyway. On the other side, code is much, much shorter and therefore it is easy to understand and to adopt further to everyone's needs.

Source code for this class is freely available at GitHub repository.

user645859
  • 316
  • 3
  • 7