Jump to content
We've recently updated our Privacy Statement, available here ×

Need help coloring TextField background


Recommended Posts

First things first, I'm not talking about the background color you can set using the color chooser in the appearance tab of the object itself. What I want to know is if there is a way to fill the whole objects background with a color similar to this expression.

 

"<style backcolor='" + $F{strColorHex} + "'>" + $F{strShownText} + "</style>"

 

This colorizes only the part of the TextField where text would be visible. So ift "strShownText" would be null, nothing would be colored even though strColorHex has a value. Basically what this expression does is coloring the textbackground. But I want one to color the fieldbackground - is that possible somehow?

Link to comment
Share on other sites

In case you need the element's background to be filled only when a condition is met, you could use either a conditional style, or the following dynamic style properties at element level:

  • net.sf.jasperreports.style.backcolor
  • net.sf.jasperreports.style.mode

More on dynamic style properties you can find out here: http://jasperreports.sourceforge.net/config.reference.html#net.sf.jasperreports.style.{style_property_suffix}

  • Thanks 1
Link to comment
Share on other sites

Hello shertage,

 

conditional styles won't work as the color doesn't change by specific conditions but according to the $F{strColorHex} value ... which can be any RGB value. And I don't really want to include a conditional style for all of them ;)

 

According to my example above, what would I need to put into the expression field to realize the fieldbackground to change color? Like I said, in my example it colors the textbackground.

So if we say ... value of strColorHex is "#000000" and strShownText has the value "Test" we would not be able to read "Test" because text color is black and the whole text background will be black too. So far correct. If strShownText gets the value "" (empty String) nothing would get colored and the TextField would still be white, which isn't what I need.

Link to comment
Share on other sites

Hi,

please let me know if I properly understood the case:

- if $F{strShownText} is null or empty, the element background should be visible, colored as $F{strColorHex}

- if $F{strShownText} is not empty (let's say if $F{strShownText} = "Test"), the background should not be displayed, in order to make the text visible

If it's correct, then you could set the following properties for the element:

<propertyExpression name="net.sf.jasperreports.style.backcolor"><![CDATA[$F{strColorHex}]]></propertyExpression>

<propertyExpression name="net.sf.jasperreports.style.mode"><![CDATA[$F{strShownText} == null || $F{strShownText}.equals("") ? "Opaque" : "Transparent"]]></propertyExpression>

In case you need the background to be always displayed, then set only the net.sf.jasperreports.style.backcolor property as shown above, and make sure the texfield mode is always Opaque.

 

Link to comment
Share on other sites

Ok, I didn't explain correctly.

 

The TextField background should always be colored in the color the strColorHex says. The field $F{strColorHex} will never be empty or null, but always be "#000000" if strShownText is empty or null.

 

I always want to read strShownText (if it's not empty or null) but also always want the complete field background to be colored exactly in the color of strColorHex. This may be possible with conditional styles, but for all RGB colors this would simply be too much. Thus the expression from above.

 

"<style backcolor='" + $F{strColorHex} + "'>" + $F{strShownText} + "</style>" is directly inserted into the expression field of the TextField itself and sadly only works if strShownText is not empty or null and even then does strange stuff ...

 

As you can see my dynamic coloring (colors come from my DB) works with the expression. The problem being clearly that only background behind text gets colored. What I'd like to get is the turquoise colored part to have the color the little colored parts have - as this is the full width of the text field.

 

In the red border you see ... well, nothing. As "Text" is an empty String there. And then the background color behind the text is gone as well ... hope I got it better this time?^^°/sites/default/files/images/ForJasperForum.png

Link to comment
Share on other sites

Thank you for this detailed explanation. :)

To get the same dynamic color for the element background as in highlighted text, just set the property for the textfield:

<propertyExpression name="net.sf.jasperreports.style.backcolor"><![CDATA[$F{strColorHex}]]></propertyExpression>

Make sure the element's mode is set as 'Opaque'. Also make sure the element gets always printed (there is no print when expression set for this element)

These above settings should solve the problem.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...