How to put a List from Java to a Report

1

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
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports <a target="_blank" href="http://jasperreports.sourceforge.net/xsd/jasperreport.xsd""><a href="http://jasperreports.sourceforge.net/xsd/jasperreport.xsd"" target="_blank">http://jasperreports.sourceforge.net/xsd/jasperreport.xsd"</a></a> name="JFreeChart" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="30" bottomMargin="30">
	<property name="ireport.scriptlethandling" value="0"/>
	<property name="ireport.encoding" value="UTF-8"/>
	<property name="ireport.zoom" value="1.0"/>
	<property name="ireport.x" value="0"/>
	<property name="ireport.y" value="0"/>
	<import value="net.sf.jasperreports.engine.*"/>
	<import value="java.util.*"/>
	<import value="net.sf.jasperreports.engine.data.*"/>
 
	<field name="speed" class="java.util.List"/>
	<field name="name" class="java.lang.String"/>
	<field name="age" class="java.lang.Integer"/>
 
	<background>
		<band splitType="Stretch"/>
	</background>
	<title>
	  <band height="52" splitType="Stretch">
	    <staticText>
	      <reportElement x="26" y="0" width="289" height="46"/>
		<textElement>
		  <font size="28"/>
	        </textElement>
		<text><![CDATA[Title of Main Report]]></text>
	    </staticText>
	  </band>
	</title>
	<pageHeader>
	  <band height="106" splitType="Stretch">
	    <textField>
	      <reportElement x="81" y="0" width="100" height="20"/>
		<textElement/>
		<textFieldExpression class="java.lang.String"><![CDATA[$F{title}]]></textFieldExpression>
	    </textField>
	    <textField>
	      <reportElement x="81" y="20" width="100" height="20"/>
	        <textElement/>
		<textFieldExpression class="java.lang.Integer"><![CDATA[$F{age}]]></textFieldExpression>
	    </textField>
	  </band>
	</pageHeader>
	<columnHeader>
	  <band height="5" splitType="Stretch"/>
	</columnHeader>
	<detail>
	  <band height="26" splitType="Stretch">
             <textField>
                <reportElement x="0" y="0" width="100" height="20"/>
                <textElement/>
                <textFieldExpression class="java.<b>????</b>"><![CDATA[$F{speed}]]></textFieldExpression>
            </textField>
           </band>
	</detail>
	<columnFooter>
	  <band splitType="Stretch"/>
	</columnFooter>
	<pageFooter>
	  <band height="7" splitType="Stretch"/>
	</pageFooter>
	<summary>
	  <band height="105" splitType="Stretch">
          </band>
	</summary>
</jasperReport>



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
nmau's picture
49
Joined: Jun 8 2010 - 2:19am
Last seen: 4 years 9 months ago

6 Answers:

-1

 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  :)

illogic_code's picture
Joined: Dec 6 2008 - 11:15am
Last seen: 4 years 3 months ago
1

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);
...</td></tr></tbody></table>
marciopd's picture
Joined: Jul 16 2010 - 9:30pm
Last seen: 8 years 8 months ago
0

Thanks a lot for your answers.

I try the example of marciopd.

nmau's picture
49
Joined: Jun 8 2010 - 2:19am
Last seen: 4 years 9 months ago
0

 

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:
Master:
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports <a target="_blank" href="http://jasperreports.sourceforge.net/xsd/jasperreport.xsd""><a href="http://jasperreports.sourceforge.net/xsd/jasperreport.xsd"" target="_blank">http://jasperreports.sourceforge.net/xsd/jasperreport.xsd"</a></a> name="Json" pageWidth="612" pageHeight="792" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
	<property name="ireport.zoom" value="1.6105100000000008"/>
	<property name="ireport.x" value="0"/>
	<property name="ireport.y" value="0"/>
	<import value="net.sf.jasperreports.engine.*"/>
	<import value="java.util.*"/>
	<import value="net.sf.jasperreports.engine.data.*"/>
	<subDataset name="dataset1"/>
	<field name="speed" class="java.util.List"/>
	<group name="Speed">
		<groupHeader>
			<band height="10" splitType="Stretch">
				<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["/home/mau/workspace/JasperTest/null/jasperreports/Speed.jasper"]]></subreportExpression>
				</subreport>
			</band>
		</groupHeader>
	</group>
	<background>
		<band splitType="Stretch"/>
	</background>
	<title>
		<band height="45" splitType="Stretch">
			<staticText>
				<reportElement x="0" y="0" width="349" height="40"/>
				<textElement>
					<font size="24"/>
				</textElement>
				<text><![CDATA[Title Of Master]]></text>
			</staticText>
			<textField>
				<reportElement x="269" y="23" width="100" height="20"/>
				<textElement/>
				<textFieldExpression class="java.lang.String"><![CDATA[""+$F{speed}]]></textFieldExpression>
			</textField>
		</band>
	</title>
	<pageHeader>
		<band height="8" splitType="Stretch"/>
	</pageHeader>
	<columnHeader>
		<band splitType="Stretch"/>
	</columnHeader>
	<detail>
		<band height="6" splitType="Stretch"/>
	</detail>
	<columnFooter>
		<band splitType="Stretch"/>
	</columnFooter>
	<pageFooter>
		<band splitType="Stretch"/>
	</pageFooter>
	<summary>
		<band splitType="Stretch"/>
	</summary>
</jasperReport>
 
Subreport:
 
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports <a target="_blank" href="http://jasperreports.sourceforge.net/xsd/jasperreport.xsd""><a href="http://jasperreports.sourceforge.net/xsd/jasperreport.xsd"" target="_blank">http://jasperreports.sourceforge.net/xsd/jasperreport.xsd"</a></a> name="Json" pageWidth="612" pageHeight="792" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
	<property name="ireport.zoom" value="1.0"/>
	<property name="ireport.x" value="0"/>
	<property name="ireport.y" value="0"/>
	<import value="net.sf.jasperreports.engine.*"/>
	<import value="java.util.*"/>
	<import value="net.sf.jasperreports.engine.data.*"/>
	<subDataset name="dataset1"/>
	<field name="speed" class="java.lang.Double"/>
	<background>
		<band splitType="Stretch"/>
	</background>
	<title>
		<band height="47" splitType="Stretch">
			<staticText>
				<reportElement x="0" y="0" width="349" height="40"/>
				<textElement>
					<font size="24"/>
				</textElement>
				<text><![CDATA[Speed Report]]></text>
			</staticText>
		</band>
	</title>
	<pageHeader>
		<band height="6" splitType="Stretch"/>
	</pageHeader>
	<columnHeader>
		<band height="21" splitType="Stretch">
			<staticText>
				<reportElement x="10" y="0" width="100" height="20"/>
				<textElement verticalAlignment="Bottom"/>
				<text><![CDATA[Speed]]></text>
			</staticText>
		</band>
	</columnHeader>
	<detail>
		<band height="21" splitType="Stretch">
			<textField>
				<reportElement x="10" y="1" width="100" height="20"/>
				<textElement/>
				<textFieldExpression class="java.lang.Double"><![CDATA[$F{speed}]]></textFieldExpression>
			</textField>
		</band>
	</detail>
	<columnFooter>
		<band height="8" splitType="Stretch"/>
	</columnFooter>
	<pageFooter>
		<band height="5" splitType="Stretch"/>
	</pageFooter>
	<summary>
		<band height="5" splitType="Stretch"/>
	</summary>
</jasperReport>


Post Edited by nmau at 07/26/2010 10:06
nmau's picture
49
Joined: Jun 8 2010 - 2:19am
Last seen: 4 years 9 months ago
0
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.
marciopd's picture
Joined: Jul 16 2010 - 9:30pm
Last seen: 8 years 8 months ago
1

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.

nmau's picture
49
Joined: Jun 8 2010 - 2:19am
Last seen: 4 years 9 months ago
Feedback
randomness