Change text field data color (Foreground color) based on condition in JasperReports
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>
Formatting field data color based on condition
There's a discussion at the JasperForge forums on the topic, this is probably the most relevant part:
First drag and drop the field that you want to have dyanamic color twice. Change background color for first field to Yellow and the second field to Red. Now right click Yellow field, click Properties, select Common tab and write this code in the 'Print when expression' box: (make sure to replace myconditionalfield with your actual field name) $F{MyConditinalField}.intValue()>= 5 && $F{MyConditinalField}.intValue()<10?Boolean.TRUE:Boolean.FALSE //if >=5 and <10 then show Yellow field //Code for Red field at the same place just like above $F{MyConditinalField}.intValue()>= 10?Boolean.TRUE:Boolean.FALSE Hope this helps.
It's not exactly what you need, but might likely be close enough to push you in the right direction.
How to create dynamic color for text field?
Maybe there is an option:
You need to set markup
to style
.
And then use an expression in the text field:
F{value1}=="GREEN"?$F{value1}:"<style backcolor='red'>"+$F{value1}+"</style>"
You can also add conditions and set multiple colors:
F{value1}=="GREEN"?"<style backcolor='green'>"+$F{value1}+"</style>":"<style backcolor='red'>"+$F{value1}+"</style>"
Maybe this will work for you.
How to set background color from parameter in Jasper Report designer
You can do that with net.sf.jasperreports.style.* element level properties (for which you can have expressions as values).
In your case you would need
<textField>
<reportElement ...>
<propertyExpression name="net.sf.jasperreports.style.backcolor">$P{someColor}</propertyExpression>
</reportElement>
...
How to create property with condition / dynamic foreground color?
The concept of using net.sf.jasperreports.style.forecolor is working well with propertyExpression at practice.
Single report with parameters
Here is small template with parameters used at propertyExpression:
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="Style forecolor" pageWidth="595" pageHeight="842" whenNoDataType="NoDataSection" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
<parameter name="color" class="java.lang.String">
<defaultValueExpression><![CDATA["#E3106B"]]></defaultValueExpression>
</parameter>
<parameter name="isApplyColor" class="java.lang.Boolean">
<defaultValueExpression><![CDATA[true]]></defaultValueExpression>
</parameter>
<parameter name="DEFAULT_COLOR" class="java.lang.String" >
<defaultValueExpression><![CDATA["#000000"]]></defaultValueExpression>
</parameter>
<title>
<band height="79" splitType="Stretch">
<textField>
<reportElement x="211" y="39" width="100" height="30" uuid="4af3eb91-2334-40fe-a77a-45378fe93210">
<propertyExpression name="net.sf.jasperreports.style.forecolor"><![CDATA[$P{isApplyColor} ? $P{color} : $P{DEFAULT_COLOR}]]></propertyExpression>
</reportElement>
<textFieldExpression><![CDATA["Text Field"]]></textFieldExpression>
</textField>
</band>
</title>
</jasperReport>
The output result generated at JSS looks like this:
I used "#000000" (black) as default color.
Looks like you need to check and fix the value of $P{evenRow}
Sample with subreport and master report.
I used subreport with parameters at propertyExpression like at first example. This report is using "external" parameter, but there is no difference - it is just a parameter
Subreport's jrxml:
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="Subreport" pageWidth="595" pageHeight="842" whenNoDataType="NoDataSection" columnWidth="595" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
<parameter name="color" class="java.lang.String">
<defaultValueExpression><![CDATA["#E3106B"]]></defaultValueExpression>
</parameter>
<parameter name="DEFAULT_COLOR" class="java.lang.String">
<defaultValueExpression><![CDATA["#000000"]]></defaultValueExpression>
</parameter>
<parameter name="evenRow" class="java.lang.Integer"/>
<title>
<band height="20" splitType="Stretch">
<textField>
<reportElement x="130" y="0" width="100" height="20">
<property name="com.jaspersoft.studio.unit.height" value="pixel"/>
<propertyExpression name="net.sf.jasperreports.style.forecolor"><![CDATA[$P{evenRow} == 0 ? $P{color} : $P{DEFAULT_COLOR}]]></propertyExpression>
</reportElement>
<textFieldExpression><![CDATA["Text Field"]]></textFieldExpression>
</textField>
<textField>
<reportElement x="20" y="0" width="100" height="20"/>
<textFieldExpression><![CDATA["evenRow: " + $P{evenRow}]]></textFieldExpression>
</textField>
</band>
</title>
</jasperReport>
The 2nd textField is just for checking value of $P{evenRow}.
The design is very simple:
The main report is using this subreport
The jrxml of master report:
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="Master" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="10 empty rows"/>
<detail>
<band height="20" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="100" height="20"/>
<textFieldExpression><![CDATA[$V{REPORT_COUNT}]]></textFieldExpression>
</textField>
<subreport>
<reportElement x="130" y="0" width="200" height="20"/>
<subreportParameter name="evenRow">
<subreportParameterExpression><![CDATA[($V{REPORT_COUNT} % 2 == 0 ? 1 : 0)]]></subreportParameterExpression>
</subreportParameter>
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.JREmptyDataSource()]]></dataSourceExpression>
<subreportExpression><![CDATA["Subreport.jasper"]]></subreportExpression>
</subreport>
</band>
</detail>
</jasperReport>
The textField is used for showing row number.
The desing looks like:
The output result at JSS:
Related Topics
Spring Qualifier and Property Placeholder
Exclude @Component from @Componentscan
Java: How to Access a Class's Field by a Name Stored in a Variable
Registering and Using a Custom Java.Net.Url Protocol
Jersey Maven Quickstart Archetype in Eclipse
What's in an Eclipse .Classpath/.Project File
How to Use Java to Read from a File That Is Actively Being Written To
Differences Between "Java -Cp" and "Java -Jar"
Recursively List All Files Within a Directory Using Nio.File.Directorystream;
Generics:List<? Extends Animal> Is Same as List<Animal>
Java Http Client Request with Defined Timeout
How to Identify End of Inputstream in Java
How to Create Splash Screen with Transparent Background in Javafx
Itext Merge Documents with Acrofields
Java Swing: How to Implement a Login Screen Before Showing a Jframe