When a JasperViewer appear and I close it, the mai

2019-02-13 17:03发布

问题:

This question already has an answer here:

  • How to prevent parent frame closed when closing child frame (Java + iReport)? 1 answer

When a JasperViewer appear and I close it, the main frame / parent also closed. How to prevent this?

This is my code..

 private void cmdprintidMouseClicked(java.awt.event.MouseEvent evt) {                                        
        // TODO add your handling code here:
        try {
            JasperDesign jasperDesign = JRXmlLoader.load("report12.jrxml");
            String sql = "select * from db1 where Company LIKE '" + txtcompany.getText() + "%'";
            JRDesignQuery newQuery = new JRDesignQuery();
            newQuery.setText(sql);
            jasperDesign.setQuery(newQuery);
            JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
            JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, conn);
            JasperViewer.viewReport(jasperPrint);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e);
        }

回答1:

Change this: JasperViewer.viewReport(jasperPrint);

to JasperViewer.viewReport(jasperPrint, false); This will work properly.



回答2:

No need to do anything, other than to call the alternative:

JasperViewer(jasperPrint, **false**);
JasperViewer.viewReport(jasperPrint, **isExitOnClose**);

The JasperViewer has alternative constructor/method call which receives boolean param: exitOnClose

I don't know if you've found your own way around but I think this is the best one.



回答3:

JasperViewer(jasperPrint, false);    

You just have to pass false with jasperviewer so the parent window won't close.



回答4:

Change:

JasperViewer.viewReport(jasperPrint);

To:

JasperViewer.viewReport(jasperPrint);
JasperViewer.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

It would seem theJasperViewer is using JFrame.EXIT_ON_CLOSE which will cause System.exit(n) to be called, thus ending the JVM.

By using JFrame.DISPOSE_ON_CLOSE instead, only that frame is ended & disposed of.