Change text field data color (Foreground color) based on condition in JasperReports
Asked Answered
T

1

15

I'm new to JasperReports. I'm designing a report using iReport. I have three values x,y,z. If z < y then the data color for z should be changed to 'black' & if z > x then data color of z should be changed to 'red'. Please tell me how to do it.

I am using JDeveloper to develop desktop app. and iReport to design JasperReport.

Tempa answered 6/1, 2012 at 6:57 Comment(1)
Another solution: #40585860Wolpert
C
19

You can use Conditional styles for solving this issue.

The sample:

<style name="ZFieldStyle">
    <conditionalStyle>
        <conditionExpression><![CDATA[$F{Z} < $F{Y}]]></conditionExpression>
        <style forecolor="#000000"/>
    </conditionalStyle>
    <conditionalStyle>
        <conditionExpression><![CDATA[$F{Z}>$F{X}]]></conditionExpression>
        <style forecolor="#FF0000"/>
    </conditionalStyle>
</style>
...
<field name="X" class="java.lang.Integer"/>
<field name="Y" class="java.lang.Integer"/>
<field name="Z" class="java.lang.Integer"/>
...
<textField>
    <reportElement style="ZFieldStyle" x="200" y="0" width="100" height="20"/>
    <textElement/>
    <textFieldExpression><![CDATA[$F{Z}]]></textFieldExpression>
</textField>
Catrinacatriona answered 6/1, 2012 at 12:4 Comment(2)
A few additional questions. How come I can't do the same with backcolor? Also, when I do this, the cell border color also changes... is there a way to prevent this?Deguzman
You can use the backcolor property in the styleQuent

© 2022 - 2024 — McMap. All rights reserved.