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
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
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.
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
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.
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,
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,
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.
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
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
RSS feed for comments on this post. TrackBack
Hi.,
It is good but ., plz insert the lines which shows the syntax i unable to get it.,
Thanks in Advance
Srikanth