pulpet112 pulpet112 - 1 year ago 121
Java Question

Adding FilterExpression in JasperReports with help of Java code

I am developing a tool in Java which enable to make some automations in JasperReports (automatically printing, setting parameters etc..) I use mostly JasperReport library. Now I have to set Filter Expression using Java code. I know how to prepare parameters, I make this in this way:

Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put("filter", "where name like '%.'");

and then, I pass this parameters while generating PDF report:

JasperPrint pdf = JasperFillManager.fillReport(jasperReport, parameters, connection);

If there is such a simple way to modify parameters, I guess there is similar way to modify filter expression. I've been searching this for a long time but I found nothing.

Answer Source

The report filter expression (or any other expression for that matter) can only be set/changed in JasperDesign objects (see the setFilterExpression method). The JasperDesign object would have to be recompiled into a JasperReport.

An alternative to the filter expression is the JRParameter.FILTER builtin parameter, which takes as value a net.sf.jasperreports.engine.DatasetFilter instance. JasperReports includes a DatasetFilter implementation called FieldFilter which is used internally for interactive filtering, but it might not suite your needs, in which case you would need to write your own DatasetFilter implementation.