Nirmal
Nirmal

Reputation: 4829

How can I apply dynamic forecolor to text-field in JasperReport

I have a textfield in the jasperreport. Now I need to pass the text-color of that field dynamically.

I have tried using iReport designer but couldn't find any relevant solutions so far.

Is it possible to achieve this ?

Upvotes: 2

Views: 5580

Answers (1)

Alex K
Alex K

Reputation: 22867

You can use Style with conditions. Or you can use different elements (textField, for example) with visibility condition - printWhenExpression.

For more details you can see this discussion.

The sample of using Style. Sets forecolor property of "Product position" textField to red when product's cost is greater than 12.0 and less or equal than 14.0 and sets it to green if cost greater than 14.0:

<style name="ColoredField">
    <conditionalStyle>
        <conditionExpression><![CDATA[Boolean.valueOf($F{PRODUCT_COST} > 12.0 && $F{PRODUCT_COST} <= 14.0)]]></conditionExpression>
        <style forecolor="#FF0000"/>
    </conditionalStyle>
    <conditionalStyle>
        <conditionExpression><![CDATA[Boolean.valueOf($F{PRODUCT_COST} > 14.0)]]></conditionExpression>
        <style forecolor="#00FF99"/>
    </conditionalStyle>
</style>
 ...
<detail>
    <band height="21" splitType="Stretch">
        <textField>
            <reportElement style="ColoredField" x="78" y="1" width="100" height="20"/>
            <textElement/>
            <textFieldExpression><![CDATA[$F{PRODUCT_NAME}]]></textFieldExpression>
        </textField>

Upvotes: 2

Related Questions