iReport Ultimate Guide - Nimsoft Library

iReport Ultimate Guide - Nimsoft Library iReport Ultimate Guide - Nimsoft Library

13.07.2015 Views

iReport Ultimate GuideFigure 10-1The Subreport elementAt design time the element will be rendered as a rectangle (Figure 10-2).Figure 10-2Subreport element placed in the Title bandIt is not necessary that the Subreport element be exactly as large as the report we will use as subreport; the element dimensionsare not really meaningful because the subreport will occupy all the space that it needs. You can think of the Subreport elementas a place holder defining the position of the top-left corner to which the subreport will be aligned. However, to avoidunexpected results, it is always better to be precise.10.1.1 Linking a Subreport to the Parent ReportTo link the subreport to the parent report you have to define three things:• How to recover the Jasper object that implements the subreport.• How to feed the object with data.• How to set the values of the subreport parameters.All this information can be defined through the Subreport element property sheet (Figure 10-3).152

SubreportsFigure 10-3Subreport element propertiesFirst, let’s take a look at how subreport parameters are set.10.1.2 Specifying the SubreportWhen we add a subreport to a report, we have to know the location of the Jasper file we’ll use to generate the subreport. Weinstruct UR to locate this file or object setting the subreport expression. As in many other contexts whereexpressions are involved, we need to set its type, that is, the kind of object returned from the expression. It’s easy to imaginethat UR will act differently according to the expression type. The type is set specifying a value for the ExpressionClass property. It must be selected from the combo box (Figure 10-3). Table 10-1 lists the possible object types.Table 10-1Possible values for Subreport ExpressionValuenet.sf.jasperreports.engine.JasperReportjava.io.InputStreamjava.net.URLjava.io.Filejava.lang.StringExplanationThe Jasper file pre-loaded in a UR object.An open stream of the Jasper file.A URL file that identifies the location of the Jasper file.A file object that identifies the Jasper file.The name of the Jasper file.If the expression is a string (java.lang.String), UR will assume that the subreport must be loaded from a Jasperfile and will try to locate the file in the same way that resources are located. Specifically, the string is at first interpreted as aURL. In case of failure (a MalformedURLException being returned), the string is interpreted as a physical path to a file; ifthe file does not exist, the string is interpreted as a resource located in the classpath. This means that using an expression oftype String means you are in some way trying to specify a file path; optionally, you can put your Jasper file in the classpathand refer to it as a resource (meaning your expression will be something like “subreport.jasper,” assuming that thedirectory containing the file subreport.jasper is in the classpath).You might be concerned about why a relative path cannot be used to locate the subreport file; in other words, why, if you havea report in c:\myreport\main_report.jasper, you cannot refer to the subreport just by using an expression like..\\mysubreports\\mysubreport.jasper. Well, you cannot do this because UR does not keep in memory the originallocation of the Jasper file that it’s working with. This makes perfect sense, considering that a Jasper object is not necessarilyloaded from a physical file.The first step in configuring a subreport element should now be clear: create an expression that can be used to load the Jasperobject to use when filling the subreport portion of the document. From experience, 99 percent of the time it will be a Jasper file153

SubreportsFigure 10-3Subreport element propertiesFirst, let’s take a look at how subreport parameters are set.10.1.2 Specifying the SubreportWhen we add a subreport to a report, we have to know the location of the Jasper file we’ll use to generate the subreport. Weinstruct UR to locate this file or object setting the subreport expression. As in many other contexts whereexpressions are involved, we need to set its type, that is, the kind of object returned from the expression. It’s easy to imaginethat UR will act differently according to the expression type. The type is set specifying a value for the ExpressionClass property. It must be selected from the combo box (Figure 10-3). Table 10-1 lists the possible object types.Table 10-1Possible values for Subreport ExpressionValuenet.sf.jasperreports.engine.JasperReportjava.io.InputStreamjava.net.URLjava.io.Filejava.lang.StringExplanationThe Jasper file pre-loaded in a UR object.An open stream of the Jasper file.A URL file that identifies the location of the Jasper file.A file object that identifies the Jasper file.The name of the Jasper file.If the expression is a string (java.lang.String), UR will assume that the subreport must be loaded from a Jasperfile and will try to locate the file in the same way that resources are located. Specifically, the string is at first interpreted as aURL. In case of failure (a MalformedURLException being returned), the string is interpreted as a physical path to a file; ifthe file does not exist, the string is interpreted as a resource located in the classpath. This means that using an expression oftype String means you are in some way trying to specify a file path; optionally, you can put your Jasper file in the classpathand refer to it as a resource (meaning your expression will be something like “subreport.jasper,” assuming that thedirectory containing the file subreport.jasper is in the classpath).You might be concerned about why a relative path cannot be used to locate the subreport file; in other words, why, if you havea report in c:\myreport\main_report.jasper, you cannot refer to the subreport just by using an expression like..\\mysubreports\\mysubreport.jasper. Well, you cannot do this because UR does not keep in memory the originallocation of the Jasper file that it’s working with. This makes perfect sense, considering that a Jasper object is not necessarilyloaded from a physical file.The first step in configuring a subreport element should now be clear: create an expression that can be used to load the Jasperobject to use when filling the subreport portion of the document. From experience, 99 percent of the time it will be a Jasper file153

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!