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 )
Comment
Subscribe
Stumble
Previous
22 Responses to Highlighting Odd and Even Rows in JasperReports
Barry Klawans
June 30th, 2006 at 7:23 pm
Recent releases of JasperReports include report styles, which make this a bit easier – you no longer have to create the rectangle.
I use iReport to create my styles – there is a “styles” pane that by default is docked with the “Library” pane. If you make it visible you can create a new style in the styles library. In the screen that pops up give the style a name (say “EvenOddRowStyle” and press “Add” under “Style Conditions”. Use one of the expressions that Brian gave and press Apply. and in the “Common” section press the “…” button next to “Backcolor” and pick the background color you want. Finally, when done with your report apply that style to all the fields in the rows you want to highlight. Just drag the style from the styles pane onto the field.
-Barry
Thomas Schmidt
October 27th, 2006 at 11:18 am
brian,
can you think of a way to set the color of a rectangle element from a field in the query?
I used
but that always got me white rects with black border.
-Thomas
Brian Burridge
October 28th, 2006 at 11:43 pm
Thomas,
You should be able to create the colored background rectangle, as I mention above, but instead of setting the Print When Expression field to be based on the variable for page count, you would use an expression for a field value.
Thomas Schmidt
October 30th, 2006 at 4:51 am
brian,
I just saw that my code sample was not visible. I try again with code tags:
Do I understand you right that I have to include a rectangle for each possible color and then don’t print those that don’t match the current field value? That’s not feasible with free color values coming from the DB. Can you please give a small code fragment with your expression.
I just want to create small colored squares in from of a line of text. If I don’t find another way I have to create little images that I include via the image tag.
-Thomas
Thomas Schmidt
October 30th, 2006 at 5:06 am
As the code fragment doesn’t come, I uploaded it to our server: beta.lutumtappert.de/temp/code.png
The following image shows what I want to achieve:
beta.lutumtappert.de/temp/sample.gif
Thanks
-Thomas
Brian Burridge
October 30th, 2006 at 2:20 pm
I didn’t realize you needed different colors for each row. That is definitely a challenge. I can’t think of a solution right now for that.
Thomas Schmidt
October 31st, 2006 at 8:59 am
My solution:
I created a scriptlet that has a method that takes a color value and creates a small colored image and returns it. Embedded into an image tag, I get my small color boxes in front of each line.
-Thomas
Omar Palomino Sandoval
November 7th, 2006 at 12:24 pm
Hi,
I think I’ve misunderstood something with the styles library’s option. I had follow the steps to apply the conditional formatting but it didn’t work. :s
ps. I’m using ireport 1.2.7.
pss. Everythin works fine with the rectangle option.
Richard
January 25th, 2007 at 5:13 am
The style option is definately more elegant, worked fine for me, the only problem with it is that it only styles the cells, so unless you expand your cells in the band so there is no space between them, then the white of the band shows through in the gaps. I hate to say it but using the rectangles is easier for the overall end result. I wish you could just apply the style to the band.
~R
nagasrikanth
February 19th, 2007 at 4:33 am
Hi.,
It is good but ., plz insert the lines which shows the syntax i unable to get it.,
Thanks in Advance
Srikanth
Steve
March 6th, 2007 at 11:45 pm
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 )
Anu
August 20th, 2007 at 2:26 am
good
WeAzLe
January 8th, 2008 at 11:51 am
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
catchme
October 7th, 2008 at 7:09 am
hi
i m using ireport
and want to generate report in alternate colors(for rows). after using idea above, i m getting rows but values r repeating one after the other.
so didnt understand where to make correction any idea
plz help
catchme33@gmail.com
Naveen
November 15th, 2008 at 6:58 am
way to print this element.
Jodel
March 3rd, 2009 at 7:27 pm
You have any idea how to highlight duplicate fields in a jasper report? That would be awesome. The scripting seems very limited compared to Crystal
wiphillyfan
January 29th, 2010 at 1:32 am
I’m using iReport and I need to have the background of a table cell change dynamically based on a field color – how can I do that? I don’t see the ability to enter an expression for the background property. If not possible via the editor, can I edit the xml manually to do it? – I’m not sure if the background color is configurable with expression. Thanks in advance for your help.
Vani
January 29th, 2010 at 1:33 am
Hi All,
am using jasper report to display Salary Sheet . where i want to highlight particular row if am getting any negative value.
Eg: – Employee ‘A’ Salary is 5000, He has taken Advance of 4000 and his loan is 1500, his Net pay will became (-500.)
When salary is processed in bluck . It I’ll be difficult to identify unless if am highlighting the negative value.
Please tell me if there any way i can do this in jasper report.
se
February 26th, 2010 at 4:22 am
wiphillyFan, I am currently doing the same thing with crosstabs. I add a style and assign this style to the crosstab “Detail/Detail” cell. I also change this cell to opaque. (You can link the style to any iReport element. I am just using the Detail/Detail cell as an example.) The style has a conditional expression so that it displays a particular background colour only when the condition is true. The conditional style is based on $V{valueMeasure}, which is the variable representing the value of each table cell in the “Detail/Detail” cell of crosstabs. See the example below for the jrxml code for the style.
<![CDATA[(($V{valueMeasure}.intValue())
se
February 26th, 2010 at 4:25 am
for some reason the code isn’t displaying properly. here’s another go at posting it:
<![CDATA[(($V{valueMeasure}.intValue())
vipul
May 3rd, 2010 at 11:59 pm
Hello,
I used Jasper report having some problem.
Actually i want to show two dynamic table with number of rows changes every time. for that i required two detail section in Jasper design page,but there is no two columnHeader section.
how to solve this problem.If any one have solution then kindly inform me my id(vipul@thebigwhale.com)
vipul
May 4th, 2010 at 12:34 am
reply if you know.