How to apply conditional formatting to your text fields

Conditional Formatting

If you wish to apply different formats to your text fields based on different conditions (for example, a variable exceeding a certain value), you can do so using "Conditional Styles".

Conditional Styles can be added to your JasperReport via iReport when adding or modifying styles (or directly within your report). Please refer to the section in the JasperReports Ultimate Guide entitled "Conditional Styles". Once you have created a Style, right click on that Style and select "Add Conditional Style". In the properties dialog for your conditional style, you can add a condition expression (Java or Groovy), and set all your style preferences. These will be applied when the condition is met, in the report elements associated with your Style (which contains the Conditional Style).

Note that a single Style can contain numerous conditions.

To associate text fields or other report elements with your Style, select your element (in iReport), and in the element's Properties, set the "Style" property to the name of the Style you created. When you run the report, the conditions contained within that style will be analyzed.

A Note About Evaluation Times for Conditional Styles

If the evaluation of the condition expression of a Style needs to be delayed (or match the evaluation time of the report elements using the Style), you must set the Report's configuration property

to true.

A Note About Order of Conditional Styles

JasperReports Library and JasperReports Server will use the FIRST condition that matches the contained value (first from top to bottom) from the list of conditional styles .

For example, if I apply the style below (named "CustSatIndexMarkup")  to a text field, a value of 6 will be displayed in red, because it is < 8. The value 6 will not be matched a second time to the third condition, even though it is < 10, because it already matched the previous condition.

And, for any elements using this style, any value less than 10 and >= 8 will be displayed in yellow.

Essentially, your Style's conditional style list reads like a series of "else if" conditions from top to bottom.

If none of the listed conditions are met, this Style will default to the style properties listed in the "<style>" tag (in this case, the forecolor is black).

<style name="CustSatIndexMarkup"
            <![CDATA[new Boolean($V{CustSatIndex_Individual} == 10)]]>
        <style forecolor="green" isBold="true"/>
            <![CDATA[new Boolean($V{CustSatIndex_Individual} < 8)]]>
        <style forecolor="red" isBold="true"/>
            <![CDATA[new Boolean($V{CustSatIndex_Individual} < 10)]]>
        <style forecolor="yellow" isBold="true"/>



I just want to leave this hear to hopefully help other newbies like me. If your conditional expression compares a string you cannot use the == syntax as in the example above. Instead you must use .equals( "string" ) or .equalsIgnoreCase( "string" ).

For example, I have a field called "PassQC" so my conditional expression in my conditional style was $F{PassQC}.equals( "Yes" ). 




Just wanted to say thanks for posting this comment. I would never have known otherwise.

I will also add for other newbies that if you're doing conditional formatting involving the background color, you have to uncheck the "transparent" checkbox on the field to get it to render properly.