NBDemoIReport

Revision as of 20:47, 6 November 2009 by Admin (Talk | contribs)
(diff) ← Older revision | Current revision (diff) | Newer revision → (diff)

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 [AppOnAPP]
      " 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".


Image:ireport3_NBDemoIReport.jpg

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


Image:ireport4_NBDemoIReport.jpg

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


Image:ireport5_NBDemoIReport.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.


File: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.


Image:ireport1_NBDemoIReport.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.



Image:ireport2_NBDemoIReport.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_NBDemoIReport.zip

Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo