[RSS]

iReport Plugin (5 Minutes)

Description

This presentation is part of the NetBeans Day World Tour and is used in the session “New & Cool in NetBeans”. This demo focuses on the iReport plugin capabilities.

Product Versions Supported

  • NetBeans 6.0.1, it does not yet work in 6.1 :-(

Points to Hit

You will be emphasizing the fact that NetBeans now has built in report building capabilities.

Demo Prep

  1. Install the iReport Plugin .
  2. Create a new Library by selecting "Tools->Libraries" and adding the following jars to a new library called "JasperReports"
  1. Add a iReport data source by doing the following.
    1. Select the "Data->Connections/Datasources" Menu item.
    2. Select the "New" button and choose "NetBeans Database JDBC Connection".
    3. Click "Next" and select the "jdbc:derby://localhost:1527/sample [app on APP]" Connection and Name it "Sample DB".

Gotchas

Be careful that the detail fields actually end up in the detail section. If they end up in the column header the Preview WILL NOT work. You can verify field locations by looking at the Outline window as it is possible for the visual designer to be out of synch with where the field actually resides. You can drag fields from section to section using the Outline window.

Demo

1. Create a "Java->Java Desktop Application" project and name it "ireport". The "Choose Application Shell" should be "Database Application" select "Next".


 ireport3.jpg

2. Select "Table" from "Create Detail Area as:" and click "Finish".


 ireport4.jpg

3. Drag a button onto the designer left of the "New" button and change the text to "Run Report".


 ireport5.jpg

4. Right-click on the "Source Packages->ireport" node and choose "New->Other...". Select "Reports->JrxmlTemplate.jrxml" and name it "customerReport".

5. Click on the "Report Query" button.


 report query.jpg

6. In the "Report Query" view, enter the following and click "OK" (alternatively, you can use the Query Designer)


SELECT
     CUSTOMER."NAME" AS CUSTOMER_NAME,
     CUSTOMER."ZIP" AS CUSTOMER_ZIP,
     CUSTOMER."ADDRESSLINE1" AS CUSTOMER_ADDRESSLINE1,
     CUSTOMER."CITY" AS CUSTOMER_CITY,
     CUSTOMER."STATE" AS CUSTOMER_STATE
FROM
     "APP"."CUSTOMER" CUSTOMER

7. From the "Outline Window", expand the "Fields" node and drag and drop "NAME", "ADDRESSLINE1", "CITY", "STATE", and "ZIP" onto the designer "detail" section. The designer should look like the following snapshot.


 ireport1.jpg

8. From the "Palette" window, drag and drop a "static text" to the "column header" above the name field and type "Name" in the text.

9. From the "Palette" window, drag and drop a "static text" to the "column header" above the address details and type "Address" in the text.

10. For both the "static text" components, in the "Properties" window select "Bold" and change the font size to 16.

11. From the "Palette" window, drag and drop a "static text" to the "title" and type "Customer Address Report" in the text.

12. Change this static text to be bold and font size 24.

13. Tighten up the design by closing the gap from the bottom up between the sections.

14. Select the "Preview" view to generate the ".jasper" file.

Explanation Point:
The final design should look something like the follow snapshot.


 ireport2.jpg

15. Right-click on the "ireport" project node and select "Properties".

16. Select "Categories:->Libraries". Add the following Libraries then select "OK".

  • "JasperReport" (Created during prep).
  • "Java DB Driver"

17. Go back to "IReportView.java" "Design" view and double-click on the "Run Report" button.

18. In the "Set Action..." dialog make the following choices.

  • Action: - Create New Action...
  • Action's method - runReport
  • Attributes->Text: Run Report

19. Click OK. Add the following code to the new "runReport" method.


        try {
            String reportName = "ireport/customerReport.jasper";
            InputStream is = this.getClass().getClassLoader().getResourceAsStream(reportName);
            Class.forName("org.apache.derby.jdbc.ClientDriver");
            Connection conn = DriverManager.getConnection("jdbc:derby://localhost:1527/sample", "app", "app");
            JasperPrint jasperPrint = JasperFillManager.fillReport(is, new HashMap(), conn);
            JasperViewer jv = new JasperViewer(jasperPrint);
            jv.setVisible(true);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(IreportView.class.getName()).log(Level.SEVERE, null, ex);
        } catch (JRException ex) {
            Logger.getLogger(IreportView.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(IreportView.class.getName()).log(Level.SEVERE, null, ex);
        }

20. Run the application and press the "Run Report" button.

Demo Cleanup

None.

Project

ireport.zip (info)

Attachments

commons-beanutils-1.7.jar Info on commons-beanutils-1.7.jar 188671 bytes
commons-collections-2.1.jar Info on commons-collections-2.1.jar 165119 bytes
commons-digester-1.7.jar Info on commons-digester-1.7.jar 139966 bytes
commons-logging-1.0.2.jar Info on commons-logging-1.0.2.jar 26388 bytes
ireport.zip Info on ireport.zip 125934 bytes
ireport1.jpg Info on ireport1.jpg 26984 bytes
ireport2.jpg Info on ireport2.jpg 31000 bytes
ireport3.jpg Info on ireport3.jpg 48366 bytes
ireport4.jpg Info on ireport4.jpg 46889 bytes
ireport5.jpg Info on ireport5.jpg 26582 bytes
jasperreports-2.0.4.jar Info on jasperreports-2.0.4.jar 1922698 bytes
report query.jpg Info on report query.jpg 5996 bytes