7

Alternate Background colour of rows in JasperReport

There are few methods to alternate background colour of rows in jasperreport.

Use conditional style.

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 following code snippet to get alternating 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>



In this example, element with this style will get red backcolor. But the presence of conditional style changes the behaviour and when rendered on an even row, the same element will have blue backcolor.

A conditional style override the properties of parent style.



OR

just put the a rectangle with a different background color and set the printwhenexpression to :

new Boolean(($V{REPORT_COUNT} mod 2) == 0)



OR

Set up a new java.lang.Integer variable called BACKGROUND. Set its Calculation Type to DistinctCount, Reset typeto None, Increment type to None, Variable Expression to a field that will always contain unique values (eg. $F{PortNumber} ), and an Initial Value Expression of new java.lang.Integer(0). That will give you an auto-incrementing variable.


In the Detail section make sure all the existing elements are set to Transparent.


Now in the Detail section of your report create a new Static Text box. Size it to cover the full extents of the detail section. If you are using multiple columns size it to the full extent of just one column. Set its background colour to be whatever you want it to be. Put a single space in the Static Text area.


To make it toggle on and off you use a Print When expression based on your BACKGROUND variable. Set it to be new java.lang.Boolean(($V{BACKGROUND}.intValue() % 2)==1)


Now all you need to do is send it to the back. Under the Format Menu choose the Send to Back option, and your done.


|

7 Comments


Thank you very much, so useful : )


iReport (I'm talking about the IDE) has a great support for styles handling.
you can define a Style with some conditionals and then apply it to your fields.

Click on 'Format' menu and select 'Styles' for more info.

http://ireport.sourceforge.net

Thank you again man

PD: the ISO8583 link it's so useful too : )


Happy to know that it helped you...


gracias. muchas gracias me sirvio de mucho.


thank for your code.


Hello,

We have developed a new gen developer friendly BI framework with some extremely unique features. Would like to give you early access & love to hear your opinion. Please do let me know of how to reach out to you. Would be launching product in 1 week from now.

Also could you please share your email details for further communication.

Regards,
Anugraha


Hi,

Thank you so much for posting this. It worked. :)

Copyright © 2009 So That I Can Remember All rights reserved. Theme by Laptop Geek. | Bloggerized by FalconHive.