当弹出窗口中显示Jasper Report时如何锁定JavaFx中的父窗口

时间:2019-10-27 16:51:39

标签: javafx jasper-reports

要在JavaFX 11中创建Jasper报告,我正在使用动态报告。当我单击按钮Jasper Report时打开,并在弹出窗口中可见。但是我可以根据需要单击按钮多次,然后报表将打开这些次数。但是我想在查看碧玉报告后锁定父窗口,以便仅在关闭弹出窗口后才能访问父窗口。我们可以在question中说明的“父子窗口”中可以实现相同的目的,但是我希望在“父子”中实现相同的目标,因为我的舞台和子窗口都是Jasper Window。下面提到了相同的代码:-

JavaFxJasperReportsDemo.java

package demo;

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

import javafx.application.Application;
import javafx.application.Platform;
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.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;

public class JavaFxJasperReportsDemo extends Application{

    @FXML
    private StackPane stackPane;

    public void start(Stage stage) throws Exception{

        try{
            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 {
            report.show(false);
        } catch (DRException e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args){
        try{
            launch(args);
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

到目前为止,我还没有基于父窗口的“可达性”的解决方案。但是,我可以为您提供一种替代解决方案,不允许用户多次单击。这样做的想法是,一旦按下按钮就禁用场景的根节点,并仅在报告窗口关闭时才启用它。不确定这是否符合您的目的。

reportButton.setOnAction(e -> {
    // Get root node of the scene
    Node sceneRoot = ((Node)e.getTarget()).getScene().getRoot();

    // First things first, disable it !!
    sceneRoot.setDisable(true);

    // Your actual method for showing report
    loadReport(e);

    // Get all current AWT windows.
    java.awt.Window[] windows = java.awt.Window.getWindows();

    // Get the recently added window (your report window) and add a close listener.
    windows[windows.length-1].addWindowListener(new java.awt.event.WindowAdapter() {
        public void windowClosing(java.awt.event.WindowEvent evt) {
            // When the window is closed, enable your root node.
            sceneRoot.setDisable(false);
        }
    });
});

注意:JasperReportBuilder的JasperViewer的内部实现是一个JFrame。