Jasper Reports Components not rendering properly in JavaFX Swing Node
Asked Answered
N

2

13

For creating Jasper Report in JavaFX 11 I am using dynamic reports. I am loading report inside Swing Node but Jasper report appears only if I will click on the stack pane area and and all other components visible only if I hover over all those components. Components and report contents not loading instantly rather than it is showing on mouse hovering and report is showing when scrolling over the Stack Pane.

As this was the bug in Java 8 and seems to be resolved but in Java 11 too I am getting the same issue.

Update

Since I am not getting any response and as suggested by kleopatra I have created minimal reproducible code. Please look into this.

JavaFxJasperReportsDemo.java

package demo;

import java.util.ArrayList;
import java.util.List;

import javax.swing.SwingUtilities;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.embed.swing.SwingNode;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import net.sf.dynamicreports.jasper.builder.JasperReportBuilder;
import net.sf.dynamicreports.report.builder.DynamicReports;
import net.sf.dynamicreports.report.builder.column.Columns;
import net.sf.dynamicreports.report.builder.component.Components;
import net.sf.dynamicreports.report.builder.datatype.DataTypes;
import net.sf.dynamicreports.report.constant.HorizontalTextAlignment;
import net.sf.dynamicreports.report.exception.DRException;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.swing.JRViewer;

public class JavaFxJasperReportsDemo extends Application{

    @FXML
    private StackPane stackPane;

    public void start(Stage stage) throws Exception{

        try{
            System.out.println("Hello");
            Parent root = FXMLLoader.load(getClass().getResource("/FXMLJavaFXJasperReportsDemo.fxml"));
            Scene scene = new Scene(root);
            stage.setScene(scene);
            stage.setTitle("Java FX Demo");
            stage.show();
            stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
                public void handle(WindowEvent arg0) {
                    Platform.exit();
                }
            });
        }
        catch (Exception e){
            throw e;
        }
    }


    @FXML
    public void loadReport(ActionEvent event) {
        JasperReportBuilder report = DynamicReports.report();
        List<DemoPOJO> lstDemoPOJOs=new ArrayList<DemoPOJO>();
        DemoPOJO demoPOJO=new DemoPOJO();
        demoPOJO.setName("ABC");
        demoPOJO.setCity("Delhi");
        lstDemoPOJOs.add(demoPOJO);
        demoPOJO = new DemoPOJO();
        demoPOJO.setName("XYZ");
        demoPOJO.setCity("Agra");
        lstDemoPOJOs.add(demoPOJO);
        report
        .columns(
                Columns.columnRowNumberColumn("S No"),
                Columns.column("Name", "name", DataTypes.stringType()),
                Columns.column("Address", "city", DataTypes.stringType())
                ).title(
                Components.text("Demo Java Fx Jasper Reports").
                setHorizontalTextAlignment(HorizontalTextAlignment.CENTER))
        .pageFooter(Components.pageXofY())
        .setDataSource(lstDemoPOJOs);

        try {
            JasperPrint jasperPrintReport=report.toJasperPrint();
            SwingNode swingNode = new SwingNode();
            AnchorPane.setTopAnchor(swingNode,0.0);
            AnchorPane.setBottomAnchor(swingNode,0.0);
            AnchorPane.setLeftAnchor(swingNode,0.0);
            AnchorPane.setRightAnchor(swingNode,0.0);
            JRViewer jrViewer=   new JRViewer(jasperPrintReport);
            SwingUtilities.invokeLater(() ->swingNode.setContent(jrViewer)
                    );
            stackPane.getChildren().add(swingNode);
        } catch (DRException e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args){
        System.out.println("Hello Main");
        try{
            launch(args);
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }
}

DemoPOJO.java

package demo;

public class DemoPOJO {

    String name;

    String city;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }
}

FXMLJavaFXJasperReportsDemo.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.StackPane?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="561.0" prefWidth="745.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="demo.JavaFxJasperReportsDemo">
   <children>
      <Label layoutX="345.0" layoutY="24.0" text="Java FX Demo Application" />
      <StackPane fx:id="stackPane" layoutX="14.0" layoutY="120.0" prefHeight="392.0" prefWidth="722.0" />
      <Button layoutX="62.0" layoutY="68.0" mnemonicParsing="false" onAction="#loadReport" text="Load Report" />
   </children>
</AnchorPane>

Dependencies I am using are:-

<dependency>
    <groupId>net.sourceforge.dynamicreports</groupId>
    <artifactId>dynamicreports-core</artifactId>
    <version>6.1.0</version>
</dependency>

<dependency>
    <groupId>org.openjfx</groupId>
    <artifactId>javafx-controls</artifactId>
    <version>11</version>
</dependency>

<dependency>
    <groupId>org.openjfx</groupId>
    <artifactId>javafx-fxml</artifactId>
    <version>11</version>
</dependency>

<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
    <version>2.2.11</version>
</dependency>

<dependency>
    <groupId>org.openjfx</groupId>
    <artifactId>javafx-swing</artifactId>
    <version>11-ea+24</version>
</dependency>

Output

  1. After clicking on load report button only one save icon is visible no report is visible. After clicking on load report button only one save icon is visible no report is visible

  2. After clicking on Stack Pane Area now Report is visible. After clicking on Stack Pane Area now Report is visible

  3. After hovering over another icon now Print icon is visible. After hovering over another icon now Print icon is visible

  4. After hovering over other icons as well they are getting visible one by one. After hovering over other icons as well they are getting visible one by one

Nicholas answered 15/10, 2019 at 14:25 Comment(23)
I am just getting upvotes but no one is giving me answer. I am very much desperate to get any solution of this question.Nicholas
you might improve your chances for an answer by providing a minimal reproducible example (mind: not your full code, but a small example written for the sole purpose of demonstrating your problem :)Monolatry
and add all relevant tags .. maybe someone from the swing/jasperreport crowd has some helpful insightMonolatry
@Monolatry added minimal reproducible code.Nicholas
I think it is swing node repainting dirty region issue.Nicholas
@petter Friberg I am using JDK 11Nicholas
Seems like same old bug, even with the test class in bug report I get the problem.Passing
@Petter Friberg How to raise bug report for Jdk??Nicholas
@Monolatry how to create bug for Jdk??Nicholas
@stackoverflowcommunity I raised the bug for the same at bugreport.java.com/bugreport. Is bugreport.java.com appropriate platform to raise the bug report.??Nicholas
bug report is created in jdk bug system having link bugs.openjdk.java.net/browse/JDK-8233052Nicholas
It has been around 25 days since I raised the bug. How much time Jdk developers take to resolve the bug?Nicholas
"How much time Jdk developers take to resolve the bug?" – anywhere from the next release to never.Height
Hi, I've running the program with jbsdk11b125_osx_x64 and I could not reproduce the issue. It shows the complete content directly. Although I getting a warning Loading FXML document with JavaFX API of version 11.0.1 by JavaFX runtime of version 10.0.2-internal...Tiu
@Tiu are you opening it in pop-up or just running my code I shared? Is the issue in JavaFx 11 not in 10.0.2-internal?Nicholas
I just run the code you shared and started it from IntelliJ on mac os with java 11. Then the application started and after a few seconds the complete stack pane was displayed.Tiu
The warning was gone when using xmlns="http://javafx.com/javafx" and xmlns:fx="http://javafx.com/fxml" in the fxml as name space (so just with out the version part).Tiu
System.getProperty("java.version") is 11-internal and System.getProperty("javafx.version") is 10.0.2-internalTiu
1) It might also be a graphics driver related issue. Some properties as starting point for troubleshooting docs.oracle.com/en/java/javase/11/troubleshoot/…. 2) Have you tried a newer release version of org.openjfx:javafx-swing, e.g. 11? 3) Using Java 11.0.5, Maven 3.6.2, and plugin org.openjfx:javafx-maven-plugin:0.0.4 it works without rendering issues (report is shown after clicking on Load Report button). The app is started with mvn clean javafx:run.Caudillo
@Tiu I just run this code in MacOS and it is working fine without doing anything extra. Could you please let me know what is the issue in Windows machine?Nicholas
Hi SatishPahuj, if I knew it, I would let you know.... Sry. I've also just tested it on mac os. But the comment from @Caudillo may help you.Tiu
thanks Vall0n!! my all configurations are same as shared by @Suboptimal. I don't have any idea about graphics related properties. Hi Suboptimal could you please help me about these properties in detail.Nicholas
@Caudillo could you please help me further in setting these 2D graphic properties as I run the same code in mac it is working fine but in windows it is not working anymore showing same issue.Nicholas
C
1

After checking the OpenJDK wiki for OpenJFX debug flags you might try following.

add in build section of the pom.xml a JVM option for OpenJFX

<plugin>
    <groupId>org.openjfx</groupId>
    <artifactId>javafx-maven-plugin</artifactId>
    <version>0.0.4</version>
    <configuration>
        <mainClass>demo.JavaFxJasperReportsDemo</mainClass>
        <options>
            <option>-Dprism.verbose=true</option>
        </options>
    </configuration>
</plugin>

execute the app with mvn javafx:run it will report the detected Prism configuration

output on my system

Prism pipeline init order: es2 sw 
Using Double Precision Marlin Rasterizer
Using dirty region optimizations
Not using texture mask for primitives
Not forcing power of 2 sizes for textures
Using hardware CLAMP_TO_ZERO mode
Opting in for HiDPI pixel scaling
Prism pipeline name = com.sun.prism.es2.ES2Pipeline
Loading ES2 native library ... prism_es2
    succeeded.
GLFactory using com.sun.prism.es2.X11GLFactory
(X) Got class = class com.sun.prism.es2.ES2Pipeline
Initialized prism pipeline: com.sun.prism.es2.ES2Pipeline
...
Graphics Vendor: Intel Open Source Technology Center
       Renderer: Mesa DRI Intel(R) Ivybridge Mobile 
        Version: 3.0 Mesa 19.3.2

it's using a hardware accelerated renderer Loading ES2 native library ... prism_es2

It's possible to force the usage of the software renderer. Change in the pom.xml

        <options>
            <option>-Dprism.verbose=true</option>
            <option>-Dprism.order=sw</option
        </options>

the output is then

Prism pipeline init order: sw 
Using Double Precision Marlin Rasterizer
Using dirty region optimizations
Not using texture mask for primitives
Not forcing power of 2 sizes for textures
Using hardware CLAMP_TO_ZERO mode
Opting in for HiDPI pixel scaling
*** Fallback to Prism SW pipeline
Prism pipeline name = com.sun.prism.sw.SWPipeline
(X) Got class = class com.sun.prism.sw.SWPipeline
Initialized prism pipeline: com.sun.prism.sw.SWPipeline
 vsync: true vpipe: false

It's using the software renderer Fallback to Prism SW pipeline.

versions used for above tests:

JDK

java version "11.0.2" 2019-01-15 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.2+9-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.2+9-LTS, mixed mode)

Maven

Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Caudillo answered 4/2, 2020 at 6:35 Comment(2)
thanks for sharing but this approach not solved the issue.Nicholas
@SatishPahuja Which concrete Java version java -version do you use? Have you tried to run the release 11 instead of 11-ea+24 of plugin org.openjfx:javafx-swing?Caudillo
R
0

I was also having a similar problem but i solved it by adding the following maven dependency to my project. I thought it could help someone. Please not i have preferred to use JRViewerFX because it solves the problem and the UI is far much better than the swing.

<dependency>
    <groupId>win.zqxu</groupId>
    <artifactId>jrviewer-fx</artifactId>
    <version>0.1.1</version>

</dependency>

Here the code section that implements it

        JRViewerFX viewer = new JRViewerFX(jasperprint_report);        

              Platform.runLater(new Runnable() {
                  @Override
                  public void run() {

                      report_scroll_pane.setContent(viewer);
                  }
              });
Revisionism answered 26/5, 2020 at 23:56 Comment(1)
We had a similar issue using Qoppa PDF Viewer inside of a SwingNode in JavaFx application. Everything was working fine with Java8, and when we switched to OpenFx11(11.0.7), it's stopped working correctly on Windows10 and MacOs machines. But then when we updated OpenFx to the OpenFX11 LTS (11.0.8), this error disappeared on MacOs but still exists on Windows machines. So my question is, do you made any progress with this issue on Windows?Iterative

© 2022 - 2024 — McMap. All rights reserved.