Java – When a JasperViewer appear and I close it, the main frame/parent also closed


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();
            JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
            JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, conn);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e);

Best Solution

Change this: JasperViewer.viewReport(jasperPrint);

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