Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jasper Reports Components not rendering properly in JavaFX Swing Node

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

like image 892
Satish Pahuja Avatar asked Oct 15 '19 14:10

Satish Pahuja


People also ask

Can I use Swing components in JavaFX?

The ability to embed JavaFX content in Swing applications has existed since the JavaFX 2.0 release. To enhance the interoperability of JavaFX and Swing, JavaFX 8 introduces a new class that provides reverse integration and enables developers to embed Swing components in JavaFX applications.

What is Primarystage in JavaFX?

The JavaFX Stage class is the top level JavaFX container. The primary Stage is constructed by the platform. Additional Stage objects may be constructed by the application. Stage objects must be constructed and modified on the JavaFX Application Thread.

What are the two parameters of stage in JavaFX divided as?

A stage has two parameters determining its position namely Width and Height. It is divided as Content Area and Decorations (Title Bar and Borders).

Can you have multiple stages displayed in a JavaFX program?

You can have multiple stages displayed in a JavaFX program.


1 Answers

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)
like image 151
SubOptimal Avatar answered Sep 27 '22 19:09

SubOptimal