iReport Ultimate Guide - Nimsoft Library
iReport Ultimate Guide - Nimsoft Library iReport Ultimate Guide - Nimsoft Library
iReport Ultimate GuideCode Example 18-1JRDefaultScriptlet, continued{public JRDefaultScriptlet() { }}public void beforeReportInit() throws JRScriptletException{}public void afterReportInit() throws JRScriptletException{}public void beforePageInit() throws JRScriptletException{}public void afterPageInit() throws JRScriptletException{}public void beforeColumnInit() throws JRScriptletException{}public void afterColumnInit() throws JRScriptletException{}public void beforeGroupInit(String groupName) throws JRScriptletException{}public void afterGroupInit(String groupName) throws JRScriptletException{}public void beforeDetailEval() throws JRScriptletException{}public void afterDetailEval() throws JRScriptletException{}}As you can see, the class is formed by a set of methods with a name composed by using the keyword after or beforefollowed by an event or action name (for example, DetailEval and PageInit). These methods map all of the events thatcan be handled by a scriptlet, which are summarized in Table 18-1.332
ScriptletsTable 18-1Report eventsEvent/MethodBefore Report InitAfter Report InitBefore Page InitAfter Page InitBefore Column InitAfter Column InitBefore Group x InitAfter Group x InitBefore Detail EvalAfter Detail EvalDescriptionThis is called before the report initialization (that is, before all variables areinitialized).This is called after all variables are initialized.This is called when a new page is created and before all variables having resettype Page are initialized.This is called when a new page is created and after all variables having resettype Page are initialized.This is called when a new column is created, before all variables having resettype Column are initialized; this event is not generated if the columns are filledhorizontally.This is called when a new column is created, after all variables having resettype Column are initialized; this event is not generated if the columns are filledhorizontally.This is called when a new group x is created and before all variables havingreset type Group and group name x are initialized.This is called when a new group x is created and after all variables havingreset type Group and group name x are initialized.This is called before a Detail band is printed and all variables are newlyevaluated.This is called after a Detail band is printed and all variables are evaluated forthe current record.Inside the scriptlet, you can refer to all of the fields, variables, and parameters using the following maps(java.util.HashMap) defined as class attributes:• fieldsMap• variablesMap• parametersMapThe groups (if present in the report) can be accessed through the attribute groups, which is an array of JRFillGroup.18.2 Creating a Simple ScriptletLike all the Java classes, to create a scriptlet you just need a simple text editor and a Java compiler. But we are not all hard coredevelopers, so let’s assume we are using an IDE (Integrated Development Environment) to do this. My favorite IDE isNetBeans (http://www.netbeans.org), but the instructions shown here should be generic enough to understand how to do thesame using another IDE.The example scriptlet I’m going to show you calculates the time taken to fill each page and prints the time in the page footer.We will store the system time when a new page is created using the method afterPageInit and provide a method to get themilliseconds past from that start time. We will then show how long the page took to be rendered by printing this value in atextfield with evaluation time Page. This is a good example of a scriptlet that can be used to profile a report execution, as well.We will start with a new Java project. If you are working on a Java application the project could be the same. In NetBeans youcreate a new project by selecting File → New Project. The window in Figure 18-1 pops up.333
- Page 281 and 282: Lists, Tables, and Barcodesidentifi
- Page 283 and 284: Lists, Tables, and BarcodesList ele
- Page 285 and 286: 14.2 TablesLists, Tables, and Barco
- Page 287 and 288: Lists, Tables, and BarcodesiReport
- Page 289 and 290: Lists, Tables, and BarcodesFigure 1
- Page 291 and 292: Lists, Tables, and BarcodesSimilarl
- Page 293 and 294: Lists, Tables, and BarcodesFigure 1
- Page 295 and 296: Lists, Tables, and BarcodesFigure 1
- Page 297 and 298: Lists, Tables, and BarcodesWhen usi
- Page 299 and 300: SubdatasetsCHAPTER 15 SUBDATASETSRe
- Page 301 and 302: SubdatasetsUR permits you to use a
- Page 303 and 304: SubdatasetsFigure 15-7Initial layou
- Page 305 and 306: SubdatasetsFigure 15-11 Pie dataset
- Page 307 and 308: CrosstabsCHAPTER 16 CROSSTABSA cros
- Page 309 and 310: CrosstabsUsing the Crosstab Wizard,
- Page 311 and 312: CrosstabsFigure 16-6Outline tree vi
- Page 313 and 314: CrosstabsFigure 16-8Adding a Row Gr
- Page 315 and 316: CrosstabsFigure 16-11 Empty row tot
- Page 317 and 318: CrosstabsFigure 16-16 Crosstab Prop
- Page 319 and 320: CrosstabsFigure 16-19 Crosstab Data
- Page 321 and 322: CrosstabsFigure 16-22 The result of
- Page 323 and 324: InternationalizationCHAPTER 17 INTE
- Page 325 and 326: InternationalizationFigure 17-2Crea
- Page 327 and 328: InternationalizationFigure 17-6New
- Page 329 and 330: InternationalizationFigure 17-7Loca
- Page 331: ScriptletsCHAPTER 18 SCRIPTLETSA sc
- Page 335 and 336: ScriptletsFigure 18-3Adding a Jar t
- Page 337 and 338: ScriptletsFigure 18-5Build output18
- Page 339 and 340: ScriptletsIf you want to add more s
- Page 341 and 342: ScriptletsThis is what you get in t
- Page 343 and 344: Scriptletsterminates the report exe
- Page 345 and 346: Additional ToolsCHAPTER 19 ADDITION
- Page 347 and 348: Additional ToolsFigure 19-3Date and
- Page 349 and 350: Additional ToolsFigure 19-4Percenta
- Page 351 and 352: 19.5 How to Run the SamplesAddition
- Page 353 and 354: Chart Theme ExampleAPPENDIX ACHART
- Page 355 and 356: Chart Theme ExampleCode Example A-1
- Page 357 and 358: IndexINDEXAAdd selected field(s) 20
- Page 359 and 360: Indexcomponents 241embedded in Java
- Page 361 and 362: Indexscriptlets 53sheets. See prope
<strong>iReport</strong> <strong>Ultimate</strong> <strong>Guide</strong>Code Example 18-1JRDefaultScriptlet, continued{public JRDefaultScriptlet() { }}public void beforeReportInit() throws JRScriptletException{}public void afterReportInit() throws JRScriptletException{}public void beforePageInit() throws JRScriptletException{}public void afterPageInit() throws JRScriptletException{}public void beforeColumnInit() throws JRScriptletException{}public void afterColumnInit() throws JRScriptletException{}public void beforeGroupInit(String groupName) throws JRScriptletException{}public void afterGroupInit(String groupName) throws JRScriptletException{}public void beforeDetailEval() throws JRScriptletException{}public void afterDetailEval() throws JRScriptletException{}}As you can see, the class is formed by a set of methods with a name composed by using the keyword after or beforefollowed by an event or action name (for example, DetailEval and PageInit). These methods map all of the events thatcan be handled by a scriptlet, which are summarized in Table 18-1.332