'How to use loops in JasperReports .jrxml file?

I am using iReport Designer to design the .jrxml file.

How can I use the for loop or if loop in .jrxml file?
Is it possible?
If yes how?



Solution 1:[1]

You may wish to use a scriptlet. It's basically a Java class that extends either of the following two classes:

In a scriptlet, you can indicate that a certain piece of code should be executed every time a report event occurs, e.g., report, page, or column initialization.

In a scriptlet, you may use loops. You may access fields, parameters, and variable values. You may also adjust variable values.

There is a chapter on scriptlets in the JasperReports Ultimate Guide.

Solution 2:[2]

Well what you can do is create a list of selected fields(which client UI selects) and pass it as a collection to JasperFillManager.fillReport and in the jrxml create a field names _THIS which will represent the value's in the List. And as you want those field to printed in loop, place them in the detail band of the report.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 animuson
Solution 2 Nilesh