10

I have a report backed by a collection of MyJavaBean.

In this report I (of course) can get the properties of MyJavaBean declaring them in the Fields and using it on the details band, so far so good.

Now I want to be able to pass this MyJavaBean as a parameter of a subreport. Look that I want to be able to pass the javabean itself, not one of its propertys.

How can I make reference to one element of my collection in the detais band?

Lucas Machado
  • 311
  • 3
  • 11
  • I don't think you can use parameters in this way. What you could do is ensure the bean is in the classpath, then instantiate and call it. The bean might have to communicate over the network to be initialized. Consider implementing a superclass for your bean that can marshall its attributes as parameters. – Dave Jarvis Jul 30 '11 at 20:32

1 Answers1

16

Referencing a bean

To declare a field that references the bean itself instead of one of its properties, set the field description to the keyword _THIS.

<field name="myJavaBean" class="com.your.package.MyJavaBean">
    <fieldDescription>_THIS</fieldDescription>
</field>

You can then pass this as a subreport parameter like any other field.

<subreportParameter name="myJavaBean">
    <subreportParameterExpression>
        <![CDATA[$F{myJavaBean}]]>
    </subreportParameterExpression>
</subreportParameter>

Methods in the bean can be called in the normal way, i.e: $F{myJavaBean}.someMethod()

Referencing a single element of the collection

Depending on what you are doing here it could be more difficult. If you want to only see the detail for the single element, set the printWhenExpression on the band to the key of the element you want. However, if you want to have some report elements reference one object in the collection while the rest of the band references another, it would probably be better for you to nest another subreport within the detail band.

Community
  • 1
  • 1
GenericJon
  • 8,746
  • 4
  • 39
  • 50
  • When I try to compile it with maven, it shows ClassNotFoundException. One more question is it possible to write ** _THIS ** **new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{_THIS}.getList1())** – Dhruvil Thaker Oct 18 '16 at 09:55
  • @DhruvilThaker Perhaps it would be worth opening a new question for your Maven issue. Regarding your code, I wouldn't expect it to work as `getList1()` is not a method of `java.util.List`, but if you changed the class to be a type with that method then I don't see why it wouldn't work. (Assuming that you are defining the datasource of a subreport, not the main report, otherwise I don't think the fields will have been resolved yet.) – GenericJon Oct 18 '16 at 13:25