Jump to content
JasperReports Library 7.0 is now available ×

Modifying report element properties in a scriptlet


dkvidera

Recommended Posts

Prior to version 1.1, you could easily change report element properties in a scriptlet by modifying the JRElement objects obtained through the JasperReport object. This no longer works because there is a parallel set of JRFillElement objects created by the JasperFillManager that now control the output. However, you can obtain and modify these JRFillElement objects in a scriptlet, if the report elements are in a group header or footer band. This does not work for elements in the predefined bands.

 

The following code demonstrates how to do this:

 

Code:
public void beforeGroupInit(String groupName) throws JRScriptletException {
if (GROUP_NAME.equals(groupName)) {
JRFillGroup group = null;
for (int g = 0; g < groups.length; g++) {
if (GROUP_NAME.equals(groups[g].getName())) {
group = groups[g];
break;
}
}

JRFillXXX element = null;
// group.getGroupHeader().getElementByKey(ELEMENT_KEY) doesn't work
JRElement[] elms = group.getGroupHeader().getElements();
for (int e = 0; e < elms.length; e++) {
if (ELEMENT_KEY.equals(elms[e].getKey())) {
element = (JRFillXXX) elms[e];
break;
}
}
element.setXXX();
...
}
}
Link to comment
Share on other sites

  • Replies 2
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Note: Once a fill element property is set, its new value remains for the rest of the report. So, if you modify a property for a special case of data, you will have to set it back for the normal cases.

 

I used this logic for images that I wanted to shrink to fit the constraining space if too big, but not enlarge to fit if small enough. If the image was small enough, I changed the size and position of the JRFillImage element to center the image; otherwise, I set the element to its 'default' size and position and RetainShape scaling shrank it properly.

Link to comment
Share on other sites

  • 9 months later...

Hello,

I have used your method to change the default backcolor and forecolor of one element, but when I generate the report the colors are always the default colors. There are other methods to call after the "setBackcolor" and "setForecolor"?

 

Thank you.

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