Jump to content

Change Rectangle Color using Scriptlet


klinder

Recommended Posts

I have figured out how to create a scriptlet and get it in my report and running. The problem I'm having is doing something simple.

Depending on a field value (txtRegCatCode) I want to change the background and foreground of a rectangle. Is this possible and if so how can I go about doing it?

I've looked in the samples and see a setVariableValue method but what I need to change isn't a variable but instead a property. I see there is a setData() method but not sure what it takes and if I could use this to do what I needed.

Below is my test scriptlet:

Code:
import net.sf.jasperreports.engine.JRDefaultScriptlet;import net.sf.jasperreports.engine.JRScriptletException;public class SecurityColor extends JRDefaultScriptlet {	public String getColor() throws JRScriptletException {		String category = (String) this.getFieldValue("txtRegCatCode");		String color = "BLACK";		if(category.compareToIgnoreCase("foo") == 0){			color = "BLUE";		}		if(category.compareToIgnoreCase("bar") == 0){			color = "RED";		}		return color;	}}
Link to comment
Share on other sites

  • Replies 2
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

Hello,

If I were you, I would use a conditionnal style. I already used that to have a list with alternate colors.

You define a style for your report and add conditionnal styles triggered by the value of your test  field having adequate background color properties.

And you attach this style to your rectangle.

I do it with iReport.

Hope this helps

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...