Conditional styles combined with some specific report built-in variables can be used to generate alternate row colors in the detail section of Jaspersoft reports.
As known, the detail section is repetitive and contains common layout information for records in the dataset. For each record in the dataset, a detail section filling process is called at fill time. Being related to records, one of the most common layouts used for the detail section is the table row layout. And like in any other table, alternate row colors might be needed.
To obtain alternate row colors, two things are necessary:
1. To know the current record number in order to determine if it’s odd or even.
2. A conditional style with specific style properties for odd or even rows.
The first requirement is accomplished with the help of report built-in counting variables:
• REPORT_COUNT – If the row color alternation does not depend on the page, column or group breaks.
• PAGE_COUNT – If each new detail page should start with the same row color.
• COLUMN_COUNT – If each new detail page column should start with the same row color
• <group_name>_COUNT – If each new group of records should start with the same row color.
• ROW_COUNT – available only for crosstabs; the ROW_COUNT variable returns the current row number in a crosstab. An example of conditional styles in a crosstab can be found in the Crosstabs sample.
How to apply alternate Background color of rows in Jasper:
A conditional style has two elements: a Boolean condition expression and a style. The style is used only if the condition evaluates to true.
We have the following code snippet to get alternate row color:
<style name=”alternateStyle” fontName=”Arial” backcolor=”red”>
<conditionalStyle>
<conditionExpression>
new Boolean($V{REPORT_COUNT}.intValue() % 2 == 0)
</conditionExpression>
<style backcolor=”blue”/>
</conditionalStyle>
</style>
DataTerrain with years of experience and reliable experts is ready to assist. We have served more than 250 customers in the US and an additional 70 customers worldwide. We are flexible in working hours and do not need any long-term binding contracts.