Jump to content
We've recently updated our Privacy Statement, available here ×

How to put a List from Java to a Report


nmau

Recommended Posts

Hi,

I have a probelm with my report. I do no use any Database. I come from Java Bans.
Thats the Java Code:
...
   Map parameters = new HashMap();
   parameters.put("name", "myName")
   parameters.put("age", myAge)
   parameters.put("List", myList)
   List masterList = new ArrayList();
   masterList.add(parameters);

   JRDataSource jrDataSource = convertReportData(masterList);
   JasperReport report = (JasperReport) JRLoader.loadObject(new File("Main.jasper"));
   JasperPrint jrPrint = JasperFillManager.fillReport(report, null, jrDataSource);
....

My JRXML looks like this:
...
       <field name="name" class="java.lang.String"/>
       <field name="age" class="java.lang.Integer"/>
       <field name="list " class="java.util.List"/> <- I'm not shure about that
...
In the detail section:
            <textField>
                <reportElement x="0" y="0" width="100" height="20"/>
                <textElement/>
                <textFieldExpression class="java.????"><![CDATA[$F{speed}]]></textFieldExpression>
            </textField>


My question is:
How do set up the jrxml-File that i can see all parameters form the list file in the detail section?

Thanks



Post Edited by nmau at 07/22/2010 13:03



Post Edited by nmau at 07/22/2010 13:03

Code: JRXML-File



Post Edited by nmau at 07/22/2010 13:04



Post Edited by nmau at 07/22/2010 13:06



Post Edited by nmau at 07/22/2010 13:12



Post Edited by nmau at 07/22/2010 13:14



Post Edited by nmau at 07/22/2010 13:28
Link to comment
Share on other sites

  • Replies 6
  • Created
  • Last Reply

Top Posters In This Topic

 i don't have sure if i understand what you need.

 But...  if it was what i am thinking, you need to pass your own values to jasperreports instead of taking them from a database?

if that's the case, this link helped me solve that problem:

 

http://www.jasperassistant.com/forum/topic/823/simple_subreport_example_using_several?p3

 

the question on this link is about subreports, but they construct datasets "by hand" and perhaps this can solve your problem.

 

if i understand wrong what you need...  please try to tell in a diferent way  :)

Link to comment
Share on other sites

I think you should take a look at JRBeanCollectionDataSource:

http://jasperreports.sourceforge.net/api/net/sf/jasperreports/engine/data/JRBeanCollectionDataSource.html

It's a datasource provided by jasper which you can initialize with a collection of your own java beans.

Look at the example code below.

Then the attributes of "YourClass" could be used as fields in the detail section of the report.

Hope it helps!

Code:
...List<YourClass> yourBeanCollection = queryData();JRDataSource beanCollectionDataSource = new JRBeanCollectionDataSource(yourBeanCollection);JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,				reportParams, beanCollectionDataSource);...
Link to comment
Share on other sites

 

Ok, the example is not that of what I'm looking for.
I want to know, how to handle a java.util.List in a jrxml-Jasper Report.

Is there a way to put the attributes from that list into a Parameter or Field. What I now is:
I can put a list in a String
List speed = [1,2,3,4,5]

Example:
  <field name="speed" class="java.util.List"/>
            <textField>
                <reportElement x="269" y="23" width="100" height="20"/>
                <textElement/>
                <textFieldExpression class="java.lang.String"><![CDATA[""+$F{speed}]]></textFieldExpression>
            </textField>

That put out in the report: [1,2,3,4,5].
My Idea was to put the speed-field in the Detail-Section and jasper realize that the speed field is a list with 5 entries and put out:
1
2
3
4
5
Because the speed-field is in the Detail-Section with repeat values.
But you can not add a field or a parameter in the report(design section)  with the class="java.util.List".
I ask me why I can create a field with the class java.util.List(at the top) and when I want to put this field in the report(design area) there is no java.util.List.
After long search in the Web I belive you can not use a list directly. You have to put the list in a subreport as datasource expression.
Is that right?

If yes, how to put the list in the subreport.
I have this in my Master.jrxml
                  <subreport isUsingCache="true">
                    <reportElement key="Report" positionType="Float" x="0" y="0" width="500" height="5"/>
                    <dataSourceExpression><![CDATA[new JRBeanCollectionDataSource((java.util.List)$F{speed})]]></dataSourceExpression>
                    <subreportExpression class="java.lang.String"><![CDATA["PathToSubreport"]]></subreportExpression>
                </subreport>

In my subreport.jrxml:
    <field name="speed" class="java.lang.Double"/>
            <textField>
                <reportElement x="10" y="1" width="100" height="20"/>
                <textElement/>
                <textFieldExpression class="java.lang.Double"><![CDATA[$F{speed}]]></textFieldExpression>
            </textField>

It alwys gives me this error:
1 [main] ERROR fill.JRFillSubreport  - Fill 27187756: exception
net.sf.jasperreports.engine.JRException: Error retrieving field value from bean : speed
    at net.sf.jasperreports.engine.data.JRAbstractBeanDataSource.getBeanProperty(JRAbstractBeanDataSource.java:123)
    at net.sf.jasperreports.engine.data.JRAbstractBeanDataSource.getFieldValue(JRAbstractBeanDataSource.java:96)
    at net.sf.jasperreports.engine.data.JRBeanCollectionDataSource.getFieldValue(JRBeanCollectionDataSource.java:100)
    at net.sf.jasperreports.engine.fill.JRFillDataset.setOldValues(JRFillDataset.java:818)
    at net.sf.jasperreports.engine.fill.JRFillDataset.next(JRFillDataset.java:782)
    at net.sf.jasperreports.engine.fill.JRBaseFiller.next(JRBaseFiller.java:1448)
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:108)
    at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:923)
    at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:845)
    at net.sf.jasperreports.engine.fill.JRFillSubreport.fillSubreport(JRFillSubreport.java:561)
    at net.sf.jasperreports.engine.fill.JRSubreportRunnable.run(JRSubreportRunnable.java:59)
    at net.sf.jasperreports.engine.fill.JRThreadSubreportRunner.run(JRThreadSubreportRunner.java:205)
    at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.NoSuchMethodException: Unknown property 'speed' on class 'class java.lang.Double'
    at org.apache.commons.beanutils.PropertyUtilsBean.getSimpleProperty(PropertyUtilsBean.java:1313)
    at org.apache.commons.beanutils.PropertyUtilsBean.getNestedProperty(PropertyUtilsBean.java:762)
    at org.apache.commons.beanutils.PropertyUtilsBean.getProperty(PropertyUtilsBean.java:837)
    at org.apache.commons.beanutils.PropertyUtils.getProperty(PropertyUtils.java:426)
    at net.sf.jasperreports.engine.data.JRAbstractBeanDataSource.getBeanProperty(JRAbstractBeanDataSource.java:111)
    ... 12 more
net.sf.jasperreports.engine.JRRuntimeException: net.sf.jasperreports.engine.JRException: Error retrieving field value from bean : speed
    at net.sf.jasperreports.engine.fill.JRFillSubreport.prepare(JRFillSubreport.java:659)
    at net.sf.jasperreports.engine.fill.JRFillElementContainer.prepareElements(JRFillElementContainer.java:327)
    at net.sf.jasperreports.engine.fill.JRFillBand.fill(JRFillBand.java:402)
    at net.sf.jasperreports.engine.fill.JRFillBand.fill(JRFillBand.java:361)
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillColumnBand(JRVerticalFiller.java:2013)
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillGroupHeader(JRVerticalFiller.java:603)
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillGroupHeaders(JRVerticalFiller.java:523)
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReportStart(JRVerticalFiller.java:250)
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:110)
    at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:923)
    at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:845)
    at net.sf.jasperreports.engine.fill.JRFiller.fillReport(JRFiller.java:85)
    at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:624)
    at ch.semafor.JasperTest.main(JasperTest.java:169)
Caused by: net.sf.jasperreports.engine.JRException: Error retrieving field value from bean : speed
    at net.sf.jasperreports.engine.data.JRAbstractBeanDataSource.getBeanProperty(JRAbstractBeanDataSource.java:123)
    at net.sf.jasperreports.engine.data.JRAbstractBeanDataSource.getFieldValue(JRAbstractBeanDataSource.java:96)
    at net.sf.jasperreports.engine.data.JRBeanCollectionDataSource.getFieldValue(JRBeanCollectionDataSource.java:100)
    at net.sf.jasperreports.engine.fill.JRFillDataset.setOldValues(JRFillDataset.java:818)
    at net.sf.jasperreports.engine.fill.JRFillDataset.next(JRFillDataset.java:782)
    at net.sf.jasperreports.engine.fill.JRBaseFiller.next(JRBaseFiller.java:1448)
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:108)
    at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:923)
    at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:845)
    at net.sf.jasperreports.engine.fill.JRFillSubreport.fillSubreport(JRFillSubreport.java:561)
    at net.sf.jasperreports.engine.fill.JRSubreportRunnable.run(JRSubreportRunnable.java:59)
    at net.sf.jasperreports.engine.fill.JRThreadSubreportRunner.run(JRThreadSubreportRunner.java:205)
    at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.NoSuchMethodException: Unknown property 'speed' on class 'class java.lang.Double'
    at org.apache.commons.beanutils.PropertyUtilsBean.getSimpleProperty(PropertyUtilsBean.java:1313)
    at org.apache.commons.beanutils.PropertyUtilsBean.getNestedProperty(PropertyUtilsBean.java:762)
    at org.apache.commons.beanutils.PropertyUtilsBean.getProperty(PropertyUtilsBean.java:837)
    at org.apache.commons.beanutils.PropertyUtils.getProperty(PropertyUtils.java:426)
    at net.sf.jasperreports.engine.data.JRAbstractBeanDataSource.getBeanProperty(JRAbstractBeanDataSource.java:111)
    ... 12 more

The complete surce is in the code section:

Code:


Post Edited by nmau at 07/26/2010 10:06
Link to comment
Share on other sites

About having to use a subreport to use the list you're right.

 

The list you're passing is a list of objects Speed, where the class Speed has getSpeed() and setSpeed() methods?

 

Did you declare the field "speed" in your subreport?

 

I think this should do it.

 

Link to comment
Share on other sites

Thanks for the answer.

I found a solution:
But quite from the beginning.
I have a JSON-File, that I read into my Java application. Unfortently Jasper can not read JSON.

Because Jasper can not handle a list directliy I have made a little trick:
For every list I make a new HashMap and put it to a main list.
All other single attributes are putted to the list at the end.
So now we have some HashMaps and in the last we have all single entrys.
Now I created a JRXML-File.
The trick is all Single entries must have the evaluationTime="Report".
Now I have all in one Report.

Source is attached

Thanks.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...