11

I would like to create a report with a custom class as follows:

public class Class1 {
  String cl1_f1;
  String cl1_f2;
}

public class Class2 {
   String cl2_f1;
   String cl2_f2;
   Class1 cl1_ob1;
}

Now I pass Class2 in the report through fields and JRBeanCollectionDataSource.

<subDataset name="myitems">
    <field name="cl2_f1" class="java.lang.String"/>
    <field name="cl2_f2" class="java.lang.String"/>
    **<field name="cl1_ob1" class="Class2"/>**  
</subDataset>

For the third parameter, I would like to mention one of its fields. For example: cl1_ob1.cl1_f1.

How can I accomplish this?

mdahlman
  • 9,204
  • 4
  • 44
  • 72
jagbandhuster
  • 677
  • 2
  • 7
  • 20

1 Answers1

17

In the Jasper report design, the field will be defined as below:

<field name="cl1_ob1" class="Class1">
   <fieldDescription><![CDATA[cl1_ob1]]></fieldDescription>
</field>

And the 2 variables of Class1 can be accessed by calling the getter method (if there is one) or you can use the variable directly, depending on it's access privileges. For Example, $F{cl1_ob1}.getCl1_f1() can be used as a text-field expression, as shown below:

<textField>
   <reportElement x="36" y="26" width="235" height="20"/>
   <textElement textAlignment="Center" verticalAlignment="Middle"/>
   <textFieldExpression><![CDATA[$F{cl1_ob1}.getCl1_f1()]]></textFieldExpression>
</textField>
bchetty
  • 2,231
  • 1
  • 19
  • 26
  • My God! Thats awesome!! Thanks bro. It works without any problems! – jagbandhuster Jan 18 '12 at 05:06
  • 1
    @bchetty I got a java.lang.ClassNotFoundException:Will not load classes from default package (Class1) – John Alexander Betts Oct 22 '13 at 19:34
  • @JohnB for java.lang.ClassNotFoundException provide full path.It's work perfectly for me.thanks bchetty. – Girish K Feb 01 '14 at 12:10
  • @JohnB Its work for one object,but if we want to pass a List of object then how we write code? – Girish K Feb 03 '14 at 09:56
  • 6
    @GirishK Great response and comments, but I'm a bit confused by what exactly you mean by provide the full path. I have provided the complete package name in the field, and the full filepath. Neither seem to work. I'm still getting a ClassNotFoundException. I have tried: FULLY.QUALIFIED.PACKAGE.NAME.class, C:/full-file-system/path/to/file/myclass.java. I have even tried importing the java classes directly into my Jasper Workspace and the using just myclass.java, myclass, and ./myclass. None of these seem to work. Clarification would be greatly appreciated. – Brandon S. Apr 28 '17 at 17:39
  • @jagbandhuster class="Class1" in field is not working , noClassDefFound for Class1. Even by providing fully qualified name ()pkg+clsname) the error still persists. – Bugs - not a bug Feb 03 '22 at 07:34
  • @BrandonS. did you find the solution? – Bugs - not a bug Feb 03 '22 at 07:34