Jump to content
JasperReports Library 7.0 is now available ×

Can Even and Odd rows have different colors?


2005 IR Help

Recommended Posts

By: Paul Meshkovsky - meshpaul

Can Even and Odd rows have different colors?

2005-03-29 16:08

 

Hello,

 

I am a new to Jasper Reports. By using iReports can a template be created to produce report having Even and Odd rows with different colors? This is one of the requirements for our reports? If yes any Ideas?

 

Thanks.

 

Paul

meshkp@pjm.com

 

 

 

 

 

By: dnelson - lizardpie

RE: Can Even and Odd rows have different colors?

2005-03-29 16:53

I've seen this done by having two filled rectangle objects to represent the background. These objects should overlay one another on the detail band with your field objects on top of these.

 

Set the background color of each rectangle to whatever your requirements are.

 

Add a variable to keep track of the row count. I suppose you could also just toggle a Boolean at each new row also.

 

Then add a Print When Expression to each rectangle. Have one print on even rows and the other on odd rows.

 

If you created a Boolean variable OddRow then the expression for the odd row rectangle would be:

 

$V{OddRow}

 

and the expression for the even row rectangle would be:

 

new Boolean($V{OddRow}.booleanValue == false))

 

 

 

 

By: dnelson - lizardpie

RE: Can Even and Odd rows have different colors?

2005-03-29 16:58

That should have been:

 

new Boolean($V{OddRow}.booleanValue() == false))

 

 

 

 

 

By: Paul Meshkovsky - meshpaul

RE: Can Even and Odd rows have different colo

2005-03-30 12:10

Is there a build it varible that I can use to determine a Row Count. If not where whould be a good place for me to create count variable in a scripplet or in a report or something else ? Some kind of hint would be helpful

 

 

 

 

By: dnelson - lizardpie

RE: Can Even and Odd rows have different colo

2005-03-30 15:37

Depending on your report you can use the built-in variable, COLUMN_COUNT.

 

Or you can create your own RowCount variable (View | Report Variables).

Variable name: RowCount

Variable class type: Integer

Calculation type: Count

Reset type: Column (depends on your specific report)

Variable expression: new Integer(1)

 

In the detail band are your 2 rectangle objects, and whatever fields you're displaying. Make sure the fields have the Transparent property checked, located on the Common tab of the Properties dialog, so the background shows through.

 

Odd row background rectangle with color1:

Print when expression:

new Boolean(($V{RowCount}.intValue()%2) == 1)

 

Even row background rectangle with color2:

Print when expression:

new Boolean(($V{RowCount}.intValue()%2) == 0)

 

 

 

 

By: Paul Meshkovsky - meshpaul

RE: Can Even and Odd rows have different colo

2005-03-31 06:33

Thanks this worked like a charm. At least up to Transparent part. The transparent did not work in iReportViewer it makes Rectangle Invisible. But what did work. Is when selecting a rectangle and -> Format -> Send to Back Worked Like a Charm.

 

 

Thank you.

 

 

 

 

By: dnelson - lizardpie

RE: Can Even and Odd rows have different colo

2005-03-31 13:43

Yeah, you have to order the components so the rects are in the background. Transparent should only be set on the text components.

 

 

 

 

By: Paul Meshkovsky - meshpaul

RE: Can Even and Odd rows have different colo

2005-04-04 08:59

The one problem that I had is that this does not work for HTML for some reason. The soulution is to copy the data coulumns set one level to A color and another level to B color and supply to each set the formula for even and odd in PrintWhenExpression. This fixes the problem.

Link to comment
Share on other sites

  • 5 years later...
  • Replies 2
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

 I was able to accomplish alternating row colors using conditional formating on the Style for the cells in the row.  

First I created a variable called "varRowCount" (same steps as above") and created a style with some conditional formatting.  There was no need to create any shapes at all.

Link to comment
Share on other sites

One improvement I use on this idea is to use a Frame element and put the conditional formatting on the Frame itself instead of on each individual element that you want to appear in that row.  Then I just drag my elements into the Frame (or cut and paste them into the Frame if I've already created the elements on the design view before I thought to create the frame).  This way, the alternating color takes on the size and shape of the frame, regardless of whether or not the different elements are actually touching or not, regardless of whether or not you have conditional printing on some of the elements (like turning printing off for repeated values), etc.  AND you get the bonus of not having to go set the Style of reach individual element inside the frame.  Just set it once and forget it.  I do so many column style reports that I have this as part of the template I use.  That keeps my reports looking standardized, with the same color every time for the alternating bands, etc.

Carl

P.S.: I just realize that I should probably mention that "Frame" is NOT the same as "Rectangle".  Rectangle is just a visible box but has no hierarchical properties.  When you put element in a Frame, they "belong" to the frame:  the Top and Left of an element are in reference to the top left corner of the frame (not the band), and you can drag a frame from one position in the band to another, and it will automatically move all the elements that are logically part of the frame.  If you're not sure if an element is really part of a frame or if it's just overlapping it visually, look a the tree in the report inspector pane.



Post Edited by cbarlow3 at 04/06/2012 15:38
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...