Thursday, October 06, 2005

Using JasperReports Scriptlet

In a few steps:
  • Create a class that extends the following class:
    • net.sf.jasperreports.engine.JRDefaultScriptlet.
  • Either:
    • Add this class name (qualified name, actually) in the scriptletClass attribute of jasperReport element of your .jrxml file;
    • Or, in case you are using iReport, declare this class in Project/Project Options menu, Scriptlet tab and "Use this scriptlet class..." option.
  • After that, if you created a new method, call it using the following syntax:
    • "$P{REPORT_SCRIPTLET}.methodName()".
      • In my case, I added to the group header the call to the method "priorityDescription()".
The scriptlet I created uses a field value to return a more complex description, what I am unable to do using only the variable declaration. It is also possible to override many of the methods that JRDefaultScriptlet provides, such as:
  • beforeReportInit()
  • afterReportInit()
  • beforeGroupInit()
  • afterGroupInit()
  • among many others...
Very interesting possibility to extend your report processing.

See an example in this more recent post. Drop me a note if you want to receive these example files and I will send them whenever I have some spare time. Thanks for your interest.
Post a Comment