From java, override expression value of a JRTextField


In my report, I need to put some placeholders for additional calculations that our client could want to see appear in the PDF. The use case is our report by default shows some account balances on a statement, but the client needs to be able to specify some additional balances that should be shown, and those balances should be 100% configuration by the client.

So for example, let's say that in a group footer I have 2 text fields A and B which respectively show the values of 2 balances stored in $V{A} and $V{B}.

I'm also adding a third field C in the footer, but I leave its expression empty so by default if won't show any text. 

The client might want to use this field to show for example the sum of A+B. Or they might want to show A-B or 2*A+2*B, whatever.

Now from my Java program where I'm compiling the report to get a JasperReport object, I'm able to locate the JRTextField element which corresponds to C. I'm able to set a few parameters on this object, but I'm not able to set the Expression because the JRExpression object only has getters and no setters. But I need to be able to override the empty expression to for example "$V{A}+$V{B}".

Again, I need to be able to do this from Java (I'm using the Library) because the client will not have access to Jaspersoft Studio and this override needs to be done dynamically.

How can I do that?

Many thanks,


spambal2's picture
Joined: Feb 20 2023 - 12:31am
Last seen: 2 days 13 hours ago

1 Answer:

Answering my own question. I didn't realize I could load my design into a JasperDesign object. From there I can override anything that I want, including a text field's expression, and then compile this design object into a JasperReport object. My mistake was wanting to modify the report directly instead of the design.


spambal2's picture
Joined: Feb 20 2023 - 12:31am
Last seen: 2 days 13 hours ago