Jump to content
Changes to the Jaspersoft community edition download ×

How to dynamically alter subreport design?

Recommended Posts

By: Ronald - hcw0818

How to dynamically alter subreport design?

2005-12-12 00:05



Is it possible to alter subreport design before filling it? I can alter others without subreports by calling

JRFont[] fonts = jasperReport.getFonts();

for (int i = 0; i < fonts.length; i++) {

JRFont font = fonts;

font.setFontSize(font.getFontSize() + addSize);


, and it works. But now I use the same way to alter a report with master&sub section, it only works to the master report, the subreport font size is not changed.


Can anybody tell me how to alter subreport design?


Thank you,






By: Barry Klawans - bklawans

RE: How to dynamically alter subreport design?

2005-12-12 09:28

You need to open up the design of each subreport and make the same changes in each one. Programatically finding all the subreports is a bit tricky though. You have to get all the JRBands in the master report (via getDetail(), getColumnHeader(), etc.) and then look through the contents of each band to find the JRElements that are a JRBaseSubreport.


Once you have each subreport, you have to actually evaluate the expression to find the reference to the actual JasperReport object to modify. I'll have to defer to Teodor on how to find and use the JRCalculator or something similar to evalulate the expression before fill time.


Finally, keep in mind that subreports loaded from a stream or URL are not readily modifiable at run time, as the JasperReport object is not loaded until the master report is filled. (They can't be loaded any earlier, since they may actually be parameterized.)


A better approach might be to add some properties to your reports that indicate all the subreports used, and a parameter used to pass that subreport. Then at run time when examining the master report you would add the code to get the properties, and for every subreport load the JasperReport, make the design changes, and pass it into your report as the specified parameter. Of course, this only works if which subreport to include isn't already parameterized.





By: Ronald - hcw0818

RE: How to dynamically alter subreport design?

2005-12-12 19:42

Dear Barry,


Thank you for your help, but I still can't fix the problem.


I tried to get all JRBaseSubreports from master report before, but I didn't find a method to change the font size in subreport as that is my target?!


There's no method in JRBaseElemets got by calling JRBand.getElemets() for changing font size, so I use JasperReport.getFonts() to get all fonts in all reports including master and sub ones, although getFonts() has been deprecated, then call setFontSize() to change its font size. Is there a better way to get JRFont as not to use the deprecated method - JasperReport.getFonts()?


Thank you again,




By: Barry Klawans - bklawans

RE: How to dynamically alter subreport design?

2005-12-13 09:03

Once you have the JRBaseSubreport element, you need to map that to an actual report. That is what makes this so hard. Since the JRBaseSubreport contains an expression, you have to somehow figure out what the expression will evaluate to, as that will typically be either a String or a JasperReport. If its a string, you need to know how to convert it into a JasperReport. The JR runtime tries to resolve a String to a JaserReport object by treating it as a URL, then a file path, then as a class name. Once you have the JasperReport for the subreport loaded, you can alter the font size.


The complexity is why I suggested putting some hints in the report as custom properties. Maybe use a standard prefix like "org.mycompany.subreport.id.reportFile" and "org.mycompany.subreport.id.parameter", so you could have entries like


<property name="com.mycompany.subreport.citylist.reportFile" value="/path/to/included/subreport.jasper"/>

<property name="com.mycompany.subreport.citylist.parameter" value="param_CitySubreport"/>


Then your code could load the report pointed to by the report file, alter the font size, and pass the JasperReport object into the master report as a parameter called "param_CitySubreport". The subreport expression would just be $P{param_CitySubreport}



Link to comment
Share on other sites

  • Replies 0
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...