8 Replies Latest reply: Sep 13, 2013 1:22 PM by 0739d4a3-81a3-471b-86a2-441d36792f50 RSS

    how can I use JasperPrint and JRViewer in javaFX 2.2, Pls Help.

    TAJDIN
      how can I use JasperPrint and JRViewer in javaFX 2.2, Pls Help.
        • 1. Re: how can I use JasperPrint and JRViewer in javaFX 2.2, Pls Help.
          0739d4a3-81a3-471b-86a2-441d36792f50

          Well, i think this is not possible. but yo can try this.

           

          InputStream reportStream = this.getClass().getResourceAsStream("reporte.jrxml");
          
          // Convert template to JasperDesign
          JasperDesign jd = JRXmlLoader.load(reportStream);
                      
          // Compile design to JasperReport
          JasperReport jr = JasperCompileManager.compileReport(jd);
                      
          // Create the JasperPrint object
          // Make sure to pass the JasperReport, report parameters, and data source
          
          JasperPrint jp = JasperFillManager.fillReport(jr,new HashMap<String, Object>(), yourjdbconnection);
          JasperViewer jv = new JasperViewer(jp);
          jv.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
          jv.setVisible(true);
          

           

          This show a swing JFrame with the JRViewer and your report inside.

          • 2. Re: how can I use JasperPrint and JRViewer in javaFX 2.2, Pls Help.
            TAJDIN

            Hi Dear Sir,

             

            Thank for your reply.

             

            Actually me building an FXML application with MVC approach and using the following classes

             

            • UserInterface.fxml
            • UserInterface.java
            • UserInterfaceController.java
            • MyiReportViewer.java (extends javax.swing.JInternalFrame)
            • ReportsMainUserInterface.java  (extends javax.swing.JFrame)
            Is there any way that I have not to use JInternalFrame and JFrame.
            Because when I run this fxml application in browser the report does not run.
            Regards
            • 3. Re: how can I use JasperPrint and JRViewer in javaFX 2.2, Pls Help.
              0739d4a3-81a3-471b-86a2-441d36792f50

              JasperViewer is a JFrame which contains a JRViewer inside, this is not using JInternalFrame.

               

              In my experiencie, you can't put JRViewer as a Node in javafx, because this extends from JPanel. In my app, i am using Stage for all windows except for report, i use JasperViewer for that.

              • 4. Re: how can I use JasperPrint and JRViewer in javaFX 2.2, Pls Help.
                James_D

                I don't use JasperReports, but as I understand it the UI part of the framework is all Swing based. In JavaFX 2.2 there is no mechanism to embed swing components into the JavaFX scene graph, so with javaFX 2.2 you won't be able to use any visual aspects of JasperReports. There's probably nothing to stop you populating reports and sending them to a printer (or exporting them), but without the ability to preview them I don't think it would be a very good user experience.

                 

                JavaFX 8 does have a SwingNode class. So if you are able to use JavaFX 8, you could create a SwingNode class and set a JRViewer as its content. However, you need to be careful with threading when mixing Swing and JavaFX, which will add considerably to your code complexity.

                 

                Is there any reason you have to use JavaFX? If you're relying heavily on JasperReports it would be much more natural to build this in Swing instead.

                • 5. Re: how can I use JasperPrint and JRViewer in javaFX 2.2, Pls Help.
                  TAJDIN

                  Dear Brother Good Morning!

                   

                  Would you like to give me your sample code where u don't use JInternal Frame to view the Reports.

                  Because I am using two classes to JFrame and JInternal Frame to view and print the report.

                   

                  Actually I add the JRViewer in JInternal Frame and then add the object of this JRViewer to  desktopPane of a JFrame.

                  My code is

                   

                  JasperPrint print = JasperFillManager.fillReport(fileName, parameter, db.con);

                  JRViewer viewer=new JRViewer(print);

                  Container c=getContentPane();

                  c.setLayout(new BorderLayout());

                  c.add(viewer);

                   

                  Then

                   

                      MyiReportViewer myiReportViewer = new MyiReportViewer("C:\\dist\\reports\\Ledger.jasper",parameters);

                      myiReportViewer.setBounds(0, 0, desktopPane.getWidth(), desktopPane.getHeight());

                      myiReportViewer.setVisible(true);

                      desktopPane.add(myiReportViewer);

                      myiReportViewer.setSelected(true);

                   

                  Then I view this JFrame with the following code

                   

                  @FXML

                      private void reportviewer(ActionEvent event) {

                         

                          try {

                              new ReportsMainUserInterface().setVisible(true);       

                          } catch (Exception ex) {

                          }   

                          }

                   

                   

                  If u say I can send u my complete code.

                   

                  Regards

                  • 6. Re: how can I use JasperPrint and JRViewer in javaFX 2.2, Pls Help.
                    TAJDIN

                    HI Dear James_D!

                     

                    I am using javaFx because it is very easy to build code for userinterface with scene builder.

                    And also I am building FXML application MVC with netbeans ide .

                    I found this very easy.

                     

                    Secondly, I can view and print jasperReports with the help of JRViewer .

                     

                    Actually I add the JRViewer in JInternal Frame and then add the object of this JRViewer to  desktopPane of a JFrame.

                    My code is

                     

                    JasperPrint print = JasperFillManager.fillReport(fileName, parameter, db.con);

                    JRViewer viewer=new JRViewer(print);

                    Container c=getContentPane();

                    c.setLayout(new BorderLayout());

                    c.add(viewer);

                     

                    Then

                     

                        MyiReportViewer myiReportViewer = new MyiReportViewer("C:\\dist\\reports\\Ledger.jasper",parameters);

                        myiReportViewer.setBounds(0, 0, desktopPane.getWidth(), desktopPane.getHeight());

                        myiReportViewer.setVisible(true);

                        desktopPane.add(myiReportViewer);

                        myiReportViewer.setSelected(true);

                     

                    Then I view this JFrame with the following code

                     

                    @FXML

                        private void reportviewer(ActionEvent event) {

                           

                            try {

                                new ReportsMainUserInterface().setVisible(true);       

                            } catch (Exception ex) {

                            }   

                            }

                     

                    I hope that you will consider my this approach and guide me that how I can run this application in web browser , because my this code works in desktop application perfectly.

                     

                    Regards

                     

                    Tanvir Ahmed

                    tanvir.tajdin@gmail.com

                    • 7. Re: how can I use JasperPrint and JRViewer in javaFX 2.2, Pls Help.
                      James_D

                      I am using javaFx because it is very easy to build code for userinterface with scene builder.

                      You should choose your technology primarily on the basis of what it can do, not on the tools available for it. JavaFX 2.2 cannot display Swing components, so it is not a good choice if you want to display a JRViewer (which is a Swing component). There are good tools for Swing, for example WindowBuider, which is included in the standard Eclipse IDE for Java Developers download.

                       

                      how I can run this application in web browser , because my this code works in desktop application perfectly.

                      Just run it as an applet (or using Java Web Start); there is no real need to switch to JavaFX to do this. The standard Java tutorial has a section on deployment which covers these options.

                       

                      • 8. Re: how can I use JasperPrint and JRViewer in javaFX 2.2, Pls Help.
                        0739d4a3-81a3-471b-86a2-441d36792f50

                        The code is the posted above. In line JasperViewer 11.  jv = new JasperViewer(jp); i create a jasperviewer. JasperViewer is a JFrame whichs contains a JRViewer as ContentPanel.


                        When you made visible de jasperviewer object. jv.setVisible(); shows the JFrame, that's all. you can put this in a UtilClass


                        public class ReportUtil {
                             public static void showReport(InputStream reportStream, HashMap<String, Object> parameters,
                                    JRDataSource dataSource) {
                                     try {
                                         JasperDesign jd = JRXmlLoader.load(reportStream);
                                         JasperReport jr = JasperCompileManager.compileReport(jd);
                                         JasperPrint jp = JasperFillManager.fillReport(jr, parameters, dataSource);
                                         JasperViewer jv = new JasperViewer(jp);
                                         jv.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                                         jv.setVisible(true);
                                     } catch (JRException e) {
                                        e.printStackTrace();
                                     }
                                 }
                        }
                        
                        

                         

                        When you can show a report you can do.

                        InputStream stream = this.getClass().getResourceAsStream("report resource location").
                        ReportUtil.showReport(stream, null, yourDataSource);