5

Background: Jaspersoft studio 6.2

How to change forecolor of a text field based on an expression.

How can I apply a expression so the font color (forecolor) will be based on the value in that field? I can't find anywhere to set a expression for the forecolor property.

Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
thotwielder
  • 1,563
  • 7
  • 44
  • 83
  • 1
    I don't think this is possible with JasperSoftStudio. It is not clean but you can put two text fields (for example with red and green color) and show one depending on an expression. – hering Apr 21 '16 at 11:14
  • Why this hasn't been built in? I understand this is open source and free, but this is a very basic feature and all the reporting tools I used have this feature... – thotwielder Apr 21 '16 at 11:17
  • I changed my opinion, have a look at my answer or the one from Petter (which is even cleaner as mine) – hering Apr 21 '16 at 11:25
  • 1
    Possible duplicate of [Change text field data color (Foreground color) based on condition in JasperReports](http://stackoverflow.com/questions/8754448/change-text-field-data-color-foreground-color-based-on-condition-in-jasperrepo) – Alex K Apr 21 '16 at 11:27

3 Answers3

6

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.

hering
  • 1,956
  • 4
  • 28
  • 43
  • 1
    Your actually works (second one is what I am looking for), although yours changed background instead of font color. I changed markup to html, and in the text expression enter something similar to yours (except it's some text ) and it works perfectly. – thotwielder Apr 21 '16 at 13:39
  • 1
    Worked like a charm. Thx – Paulo Künzel Dec 12 '19 at 16:07
5

Another possibility would be to use a style property expression in your textfield:

<textField ...>
  <reportElement ...>
      <propertyExpression name="net.sf.jasperreports.style.forecolor">
         <![CDATA["#00FF00"]]>
      </propertyExpression>
  </reportElement>
  ...
</textField>

This setting will override the current forecolor attribute for the textfield, and a green text will be printed out.

Here are listed all dynamic style properties available for report elements.

shertage
  • 236
  • 2
  • 4
  • This is not dynamic. So the original question is not answered properly. Maybe there is a way to hava a conditional propertyExpression or something similar – DavidDunham Jul 03 '17 at 12:01
  • @shertage: you should have mentioned that it is possible to use <![CDATA[$F{color}]]> – Turo Dec 21 '17 at 12:22
3

You use conditionalStyle, to achieve this

Example

<style name="myStyle" forecolor="#0000FF">
    <conditionalStyle>
        <conditionExpression><![CDATA[$F{myField}<0]]></conditionExpression>
        <style forecolor="#CC0000"/>
    </conditionalStyle>
</style>

Then set the style to the textField when you like to use it

<textField>
    <reportElement style="myStyle" x="448" y="5" width="100" height="20" uuid="b75e4497-e952-4051-8640-2f6b498dd152"/>
    <textFieldExpression><![CDATA["Hello world"]]></textFieldExpression>
</textField>

In JasperSoft Studio, right click on style in outline to create "Conditional Style" and set the properties in the properties tab

JasperSoft Studio

Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
  • What are the codes for and how to use the code? Are we talking Jaspersoft studio 6.2? And don't forget the background is 'I am new'. – thotwielder Apr 21 '16 at 13:35
  • @thotwielder The code is the jrxml code (universal for all IDE),if you click source in IDE you will see it. I have included a screen shot so you can find where the styles are also in IDE. – Petter Friberg Apr 21 '16 at 13:41
  • Hence when you use IDE it generates the jrxml code : ). – Petter Friberg Apr 21 '16 at 13:43
  • I see what you mean now, yours will work but the first one is easier imho and comes before yours, so, but thanks for the explanation. – thotwielder Apr 21 '16 at 14:55
  • @thotwielder my is the standard way, you can change all the properties of your text from (background, foreground, font, bold ecc), comes before mine??, what do you mean??, but no problem you choose what ever you like – Petter Friberg Apr 21 '16 at 15:08
  • I meant his/her is the first answer, yours is second so comes before yours – thotwielder Apr 21 '16 at 15:34