Jump to content
We've recently updated our Privacy Statement, available here ×
  • How to add user defined function in iReport


    In iReport it is very important to have a knowledge of integrating the Java code with the Jasper Report as the things do easier and provide lot of versatility to the report. Usage of Java Code in iReport is achieved by usage of scriptlets.

    Scriptlets acts as bridge to get the Java Code in the iReport, scriptlet is a Java class which extends the JRDefaultScriptlet, and in this class we could write our user defined functions which later could be easily accessed in the report.


    There are few simple steps which could be followed in order to run the User Defined Functions.

    1. Tools(in the iReport Menu bar ) ----->  Options -----> Click on Claspath Tab ------> ADD Jar -----> choose the Jar file ( This JAR is made on building the project containing the scriptlet )
    2. Then in the iReport there is a node called Scriptlet in the Report Inspector ( By Default on left ).
    3. Go to the properties of the scriptlet , there is a property called 'Scriptlet Class'
    4. Key in the the location of the scriptlet class where the user defined function is present  for instance:
    com.jasper.scriptlet.TestScriptlet -- In this scenario TestScriptlet is my class where the user defined function is written.
    (Remember the the class containing the user defined function needs to be extended by the JRDefaultScriptlet )
    5. Now you are set to use it in the report, to use in the iReport you need to use the reference of a parameter called $P{REPORT_SCRIPTLET} , this is a predefined parameter which could come handy.
     6. In order to use the user defined in the iReport, in the the expression would be given as 
     $P{REPORT_SCRIPTLET} .userDefinedFun() -  This is a user defined function which returns values from the scriptlet.
    $P{REPORT_SCRIPTLET} .userDefinedFun($F{field})  - This is also a user defined function which returns values from scriptlet, but has an input parameter from the field in the report. So this gives fluidity of the Java from Jasper to Java code.

    Here is a running sample-

    Prerequesites-
    1. JRXML File
    2. Jar File
    3. PDF Export 

    Steps to set up this Sample Report.
    1. Tools(in the iReport Menu bar )-----> Options ----- > Click on Claspath Tab ------> ADD Jar -----> choose the Jar file
    2.  Then navigate to Scriptlet node in the Report inspector, and then modify the property Scriptlet Class to 'pkg1.ConvertToWord'
    3. Then you must choose Sample Database in the datasource
    4. Then next you need to activate the the sample database from Help(in the iReport Menu bar)---->Samples---->Run Sample Database
    5. Then Run the  Report
    There is a PDF export also which would show exactly how the report looks.

    Please put in you queries/suggestions, I will be more than happy to know.
    Cheers .. Happy Coding!!

    User Feedback

    Recommended Comments

    There are no comments to display.



    Guest
    This is now closed for further comments

×
×
  • Create New...