Jump to content
Changes to the Jaspersoft community edition download ×

Set background Color from a parameter


adry

Recommended Posts

  • Replies 7
  • Created
  • Last Reply

Top Posters In This Topic

This is a solution to change a style attribute after the call to

Code:

jasperPrint = JasperFillManager.fillReport(rptfile, parameters, dataSource);

 

1. In ireports define a style that contains a default background color. Apply this style to the rectangle(s).

 

2. After calling fillReport(), find the style and change its background color attribute:

Code:
[code]
if (hexVal != null && !"".equals(hexVal)) {
Color userColor = new Color(R, G, B );
JRStyle[] styleList = jasperPrint.getStyles();
for (int j = 0; j < styleList.length; j++) {
System.out.println("style " + j + " is " +
styleList[j].getName());
if (styleList[j].getName().equals("tableHeader"«»)) {
System.out.println("found tableHeader"«»);
styleList[j].setBackcolor(userColor);
jasperPrint.addStyle(styleList[j], true);
}
}
}

 

3. Export your report.

Post edited by: niik, at: 2007/08/28 15:52

Link to comment
Share on other sites

Hi,

 

If it is about a parameter, then it means you know the color even before filling the report and the rectangle will have the same color no matter how many times it gets rendered in the report.

If so, then you could try "alter" the rectangle's color by making modifications on the compiled report template at runtime, as shown in the /demo/samples/alterdesign sample provided with the project.

 

But if you are actually looking for a solution to change the color of the rectangle with every record (dependent on a field or variable, not only parameter), then this is not possible. If the number of different colors is limited, then a conditional style could be used to make the rectangle color change based on runtime conditions.

 

I hope this helps.

Teodor

Link to comment
Share on other sites

But if you are actually looking for a solution to change the color of the rectangle with every record (dependent on a field or variable, not only parameter), then this is not possible. If the number of different colors is limited, then a conditional style could be used to make the rectangle color change based on runtime conditions.

It is possible, with a trick: I generate 1x1 Pixel images and put those into my DataSource, as Colors are not supported directly. Then I create an image field in iReport and let JasperReports fill it from the image I generated from the color information.

 

This works for me:

 

Code:
BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB«»);
img.setRGB(0, 0, color.getRGB());

 

Regards,

Mathis

Link to comment
Share on other sites

  • 12 years later...

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