Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 790
Sachin D Avatar asked Jan 06 '12 06:01

Sachin D


People also ask

How do I change font color in Jasper report?

1) Take a simple query which gives you some numeric values( in this case it is BigDecimal). 2) From the Report Inspector, right click on Report ->Properties -> Language ->Groovy (it is by default, but you need to check). 3) From the same Report Inspector, Click on Styles and add two conditional styles.

What is static text in Jrxml?

Static Text is used for text elements in the template that do not change. An example of this would be the instructions section of a routing slip.


1 Answers

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>
like image 124
Alex K Avatar answered Sep 21 '22 20:09

Alex K