How to use printWhenExpression in JasperReports

2019-02-16 08:18发布

问题:

Can somebody tell me how to use the printWhenExpression of JasperReports?

回答1:

Do you have an error relative to boolean ?

Because you need to use Boolean instead of the primitive type.

So:

$F{mesure} != "PH"
($F{userfd4}).equals("1") ? true : false 

would give cannot cast from boolean to Boolean.

( $F{mesure}.startsWith("PH") ? Boolean.TRUE:Boolean.FALSE ) 
($F{userfd4}).equals("1") ? Boolean.TRUE : Boolean.FALSE  

would be correct.

See also this example


Update Nov 2015 (7 years later)

Petter Friberg points out in the comments:

In jasper report 6.0 this is not need:
You can return either boolean or Boolean a simple expression like $F{fieldName}.equals("hello") will work.



回答2:

The other poster has done a good job of explaining the technical details of how to use it, so I'll try and explain the circumstances in which one might find it useful.

Basically, it allows you to show or hide the contents of a cell based on a boolean expression. For example, you might want to show a person's name only if that person is over 18, then in the name field, using a printwhenexpression like:

$F{age} >= 18


回答3:

You can also use the static method "Boolean.valueOf(boolean b)". It does exactly the same logic as "($F{mesure}.startsWith("PH") ? Boolean.TRUE:Boolean.FALSE)" and good rule of thumb is don't recreate the wheel.

Boolean.valueOf($F{mesure}.startsWith("PH"))

Boolean.valueOf($F{userfd4}).equals("1"))



回答4:

$P{mesure}.equals("200") should work without any additional details. It will print if the condition is true