tags: JasperReports
posted: Monday, June 19th, 2006
In order to highlight odd or even rows with a background color, in JasperReports, first create a rectangle element with a background of your color choice, and send it to the background (in JasperAssistant, right click on the rectangle, choose Arrange and then Send to Back). Second, use the following expression to set when the element should appear, by placing the expression in the Print When Expression field of the rectangle element. Where the $V{PAGE_COUNT} appears, you can use that, to base it on the row number on the page, or use $V{REPORT_COUNT} to base it on the row within the report. Also, if you are using a group, you can use $V{groupname_COUNT} where groupname is replaced with the name of your group. The expression below will then test the row number within the Page, Report, or Group (whichever you selected) and if its divisible by 2, it will display the rectangle element with the background color.
new Boolean( $V{PAGE_COUNT}.intValue() % 2 ==0 )
Comments (13)
Leave a Comment
good
Nice solution. Just thought I’d mention that you’re needlessly creating a bunch of Boolean objects though. You can do the following to avoid that:
Boolean.valueOf( $V{PAGE_COUNT}.intValue() % 2 == 0 )
RSS feed for comments on this post. TrackBack
Hi Brian,
firstly, let me thank you for the hint on how to use a different color on odd and even rows! I tried your solution and it immediatly worked for me with exporting to PDF. I wanted to use the same thing in XLS but my rows did not get colored, so I was wondering if I might do something wrong like PROPERTY_WHITE_PAGE_BACKGROUND or something like that for Excel exporting. In the end I read the comment from Barry which finally helped me also getting the feature in XLS export while defining a new style with the condition expression
Boolean.valueOf( $V{PAGE_COUNT}.intValue() % 2 == 0 )
on it without using a rectangle and a print when expression on it!
Thanks again guys, this went immediatly into my own FAQ for designing JasperReports.
Bze,
WeAzLe