Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When a JasperViewer appear and I close it, the main frame/parent also closed [duplicate]

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);
        }
like image 820
theo esguerra Avatar asked May 16 '13 06:05

theo esguerra


4 Answers

Change this: JasperViewer.viewReport(jasperPrint);

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

like image 125
Tharindu Avatar answered Nov 16 '22 06:11

Tharindu


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.

like image 35
KS Dev Avatar answered Nov 16 '22 06:11

KS Dev


JasperViewer(jasperPrint, false);    

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

like image 2
Arihant Avatar answered Nov 16 '22 05:11

Arihant


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.

like image 1
Andrew Thompson Avatar answered Nov 16 '22 05:11

Andrew Thompson