For example, you have a couple of JavaBeans (POJOs):
public class Order { private double price; private int quantity; private Product product;
and you declare the report data source like this: (yes, I like Guava)
JRBeanCollectionDataSource datasource = new JRBeanCollectionDataSource(Lists.newArrayList(ImmutableList.<Order>builder() .add(new Order(1000.2, 10, new Product("Phone"))) .add(new Order(10200.0, 2, new Product("Tv"))) .build()));
If you use this field declaration:
<field name="order" class="java.lang.Object"> <fieldDescription><![CDATA[_THIS]]></fieldDescription> </field> <field name="price" class="java.lang.Double"/> <field name="quantity" class="java.lang.Integer"/> <field name="productName" class="java.lang.String"> <fieldDescription><![CDATA[product.name]]></fieldDescription> </field>
you can use these expressions:
<textField> <reportElement x="0" y="0" width="100" height="30"/> <textFieldExpression><![CDATA[$F{price}]]></textFieldExpression> </textField> <textField> <reportElement x="100" y="0" width="100" height="30"/> <textFieldExpression><![CDATA[$F{quantity}]]></textFieldExpression> </textField> <textField> <reportElement x="200" y="0" width="100" height="30"/> <textFieldExpression><![CDATA[$F{productName}]]></textFieldExpression> </textField>
Note:
- Do not forget that getters must be publicly available.
- Further Information: JavaBean Data Sources
- A good explanation of _THIS using samples can be found in these posts:
source share