Java Reference
In-Depth Information
As can be seen in the previous Java source code, scriptlets have access to report
variables. Their value can be obtained by calling the getVariableValue()
method. In this example, we access built-in variables only; however, there is
nothing preventing scriptlets from accessing normal variables. Similarly, scriptlets
can access report fields and parameters, both built-in and custom, by calling the
getFieldValue() and getParameterValue() methods, respectively. Just like the
getVariableValue() method, both of these methods take a single String parameter
indicating the name of the field or parameter to obtain. Scriptlets can only access,
but not modify, report fields and parameters. However, scriptlets can modify report
variable values. This can be accomplished by calling the setVariableValue()
method. This method is defined in JRAbstractScriptlet class, which is always
the parent class of any scriptlet. The following example illustrates how to modify a
report variable from a scriptlet:
package net.ensode.jasperbook;
import net.sf.jasperreports.engine.JRDefaultScriptlet;
import net.sf.jasperreports.engine.JRScriptletException;
public class ReportVariableModificationScriptlet extends
JRDefaultScriptlet
{
public void afterReportInit() throws JRScriptletException
{
setVariableValue("someVar", new String(
"This value was modified by the scriptlet."));
}
}
The preceding class will modify a variable named someVar to have the value This
value was modified by the scriptlet .
Notice how the preceding scriptlet extends
JRDefaultScriptlet instead of JRAbstractScriptlet .
JRDefaultScriptlet is a convenience class included with
JasperReports. It includes empty implementations of all abstract
methods in JRAbstractScriptlet , allowing us to override
only those methods that concern our particular use case.
The following JRXML template uses the preceding scriptlet to modify the value of its
someVar variable:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE jasperReport PUBLIC "-//JasperReports//DTD Report Design//
EN" "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">
<jasperReport name="ScriptletVariableModificationReport"
 
Search WWH ::




Custom Search