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

net.sf.jasperreports.style.evaluation.time.enabled

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"
          forecolor="black"
          pattern="###0.00;-###0.00"
          isBlankWhenNull="false"
          fontSize="10"
          isBold="false"
          isItalic="false"
          isUnderline="false"
          isStrikeThrough="false">
    <conditionalStyle>
        <conditionExpression>
            <![CDATA[new Boolean($V{CustSatIndex_Individual} == 10)]]>
        </conditionExpression>
        <style forecolor="green" isBold="true"/>
    </conditionalStyle>
    <conditionalStyle>
        <conditionExpression>
            <![CDATA[new Boolean($V{CustSatIndex_Individual} < 8)]]>
        </conditionExpression>
        <style forecolor="red" isBold="true"/>
    </conditionalStyle>
    <conditionalStyle>
        <conditionExpression>
            <![CDATA[new Boolean($V{CustSatIndex_Individual} < 10)]]>
        </conditionExpression>
        <style forecolor="yellow" isBold="true"/>
    </conditionalStyle>
</style>

 

Feedback
randomness