jasper-reportsjdialogjasper-plugin

How to make jasper report top of JDialog?


I make reports using jasper reports plugin for Netbeans, I use a button in JDialog to show my jasper report. But the report shows back on my JDialog, what's the problem here?

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                                
    // TODO add your handling code here:
    Map<String, Object> params = new HashMap <>();
    try {
        Connection kon = DriverManager.getConnection(database,user,pass);
        JasperDesign jasperDesign = JRXmlLoader.load("Myreports.jrxml");
        JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
        JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, kon);
        JasperViewer jasperViewer = new JasperViewer(jasperPrint, false); 
        jasperViewer.setTitle("My Reports");
        jasperViewer.setVisible(true);
    } catch (SQLException | JRException e){
        e.printStackTrace();
    }
}

I was add setModal like this :

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                                
    // TODO add your handling code here:
    Map<String, Object> params = new HashMap <>();
    try {
        Connection kon = DriverManager.getConnection(database,user,pass);
        JasperDesign jasperDesign = JRXmlLoader.load("Myreports.jrxml");
        JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
        JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, kon);
        JasperViewer jasperViewer = new JasperViewer(jasperPrint, false); 
        jasperViewer.setTitle("My Reports");
        setModal(false);
        jasperViewer.setVisible(true);
        setModal(true);
    } catch (SQLException | JRException e){
        e.printStackTrace();
    }
}

My report show top on JDialog but no buttons in it worked.


Solution

  • You can use this code to make JR appear as JDialog:

    //after creating jasperViewer//
    jasperViewer = new JasperViewer(jasperPrint, false);
    JDialog dialog = new JDialog(this);//the owner
    dialog.setContentPane(jasperViewer.getContentPane());
    dialog.setSize(jasperViewer.getSize());
    dialog.setTitle("XXXXX");
    dialog.setIconImage(Toolkit.getDefaultToolkit().getImage(
    getClass().getResource("URL IMG")));
    dialog.setVisible(true);