RSS

Alternate Background colour of rows in JasperReport

25 Jul

By: http://sothaticanremember.blogspot.com/

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.

Advertisements
 
Leave a comment

Posted by on July 25, 2014 in JasperReport

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: