Can somebody tell me how to use the printWhenExpression
of JasperReports?
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"))
$P{mesure}.equals("200") should work without any additional details. It will print if the condition is true
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 eitherboolean
orBoolean
a simple expression like$F{fieldName}.equals("hello")
will work.
You can see a demo of that command in demo/samples/tableofcontents/reports/TocPart.jrxml
<reportElement style="Sans_Bold" positionType="Float" x="50" y="0" width="100" height="15" isRemoveLineWhenBlank="true" uuid="db8b68c6-4430-4199-8967-3ab5c077cb56">
<property name="local_mesure_unitx" value="pixel"/>
<property name="com.jaspersoft.studio.unit.x" value="px"/>
<printWhenExpression><![CDATA[$F{level} == 1]]></printWhenExpression>
</reportElement>
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