0

I am a bit new to JasperReports. I am trying to print a list of names in one of the columns in a sample report using iReport. Although the list is getting displayed correctly yet it is not giving the output format as desired.

Output Received:

Merchant Name                              Fruits Sold
Marx                                       [Fiji apple,  Kashmir apple, Shimla apple]
Stalin                                     [Fiji apple,  Kashmir apple, Shimla apple]

Whereas the Expected output should be:

Merchant Name                             Fruits Sold
Marx                                      Fiji apple
                                          Kashmir apple
                                          Shimla apple

Stalin                                    Fiji apple
                                          Kashmir apple
                                          Shimla apple

I am using a List element in report for the "fruits sold" column. And in the list frame I am putting the text field element ${fruitsForSale} from the Javabean. The List element is having the connection expression as $P{REPORT_CONNECTION} to use the same connection as the main dataset.

The Javabean class is as below:

import java.util.ArrayList;
import java.util.List;

public class FruitSeller {
  private String merchantName;
  private List<String> fruitsForSale;

  public String getMerchantName() {
    return this.merchantName;
  }

  public void setMerchantName(String merchantName) {
    this.merchantName = merchantName;
  }

  public List<String> getFruitsForSale() {
    return this.fruitsForSale;
  }
  public void setFruitsForSale(List<String> fruitsForSale) {
    this.fruitsForSale = fruitsForSale;
  }
  public static List<FruitSeller> prepareReport() {
    List fruitSellerList = new ArrayList();

    FruitSeller fruitSeller = new FruitSeller();
    fruitSeller.setMerchantName("Marx");
    List fruitsForSale = new ArrayList();
    fruitsForSale.add("Fiji apple");
    fruitsForSale.add("Kashmir apple");
    fruitsForSale.add("Shimla apple");
    fruitSeller.setFruitsForSale(fruitsForSale);
    fruitSellerList.add(fruitSeller);

    FruitSeller fruitSeller2 = new FruitSeller();
    List fruitsForSale2 = new ArrayList();
    fruitSeller2.setMerchantName("Stalin");
    fruitsForSale2.add("Fiji apple");
    fruitsForSale2.add("Kashmir apple");
    fruitsForSale2.add("Shimla apple");
    fruitSeller2.setFruitsForSale(fruitsForSale2);
    fruitSellerList.add(fruitSeller2);
    return fruitSellerList;
  }
}

And the jrxml file is as below:

<?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 http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="TaskReport" language="groovy" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="fe637f77-a273-4977-a964-127a514b9844">
    <subDataset name="dataset1" uuid="d4eb79c1-9e33-42d2-ae15-a8416f7c1cb4"/>
    <subDataset name="New Dataset 1" uuid="18df23e8-0545-4bef-aef8-804e425d990f"/>
    <subDataset name="dataset6" uuid="09a35364-9d98-4c70-9246-de2759b0d421"/>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <field name="fruitsForSale" class="java.util.List">
        <fieldDescription><![CDATA[fruitsForSale]]></fieldDescription>
    </field>
    <field name="merchantName" class="java.lang.String">
        <fieldDescription><![CDATA[merchantName]]></fieldDescription>
    </field>
    <columnHeader>
        <band height="34" splitType="Stretch">
            <staticText>
                <reportElement uuid="8dbb8a02-6cb2-404a-af92-34c129fdae11" stretchType="RelativeToBandHeight" x="0" y="0" width="262" height="34"/>
                <box>
                    <topPen lineWidth="0.5"/>
                    <leftPen lineWidth="0.5"/>
                    <bottomPen lineWidth="0.5"/>
                    <rightPen lineWidth="0.5"/>
                </box>
                <textElement verticalAlignment="Middle">
                    <font size="12" isBold="true"/>
                </textElement>
                <text><![CDATA[Merchant Name]]></text>
            </staticText>
            <staticText>
                <reportElement uuid="389f9247-f6d9-43b7-86e7-ad6aed456117" x="262" y="0" width="293" height="34"/>
                <box>
                    <topPen lineWidth="0.5"/>
                    <leftPen lineWidth="0.5"/>
                    <bottomPen lineWidth="0.5"/>
                    <rightPen lineWidth="0.5"/>
                </box>
                <textElement verticalAlignment="Middle">
                    <font size="12" isBold="true"/>
                </textElement>
                <text><![CDATA[Fruits Sold]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="44" splitType="Stretch">
            <textField>
                <reportElement uuid="94bef980-1315-418a-96f8-4a5f6bf21e9a" x="0" y="0" width="262" height="44"/>
                <box>
                    <topPen lineWidth="0.5"/>
                    <leftPen lineWidth="0.5"/>
                    <bottomPen lineWidth="0.5"/>
                    <rightPen lineWidth="0.5"/>
                </box>
                <textElement>
                    <font size="9"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{merchantName}]]></textFieldExpression>
            </textField>
            <componentElement>
                <reportElement uuid="e3c1916e-8785-4517-9f99-132edd759cc7" x="262" y="1" width="293" height="43"/>
                <jr:list xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd" printOrder="Vertical">
                    <datasetRun subDataset="dataset6" uuid="8bcd4e11-7efd-48bf-9314-fd84812879d7">
                        <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
                    </datasetRun>
                    <jr:listContents height="43" width="293"/>
                </jr:list>
            </componentElement>
            <textField>
                <reportElement uuid="1fe0b910-b7f8-48e9-aa21-5b0073d8b6f5" x="262" y="0" width="293" height="44"/>
                <box>
                    <topPen lineWidth="0.5"/>
                    <leftPen lineWidth="0.5"/>
                    <bottomPen lineWidth="0.5"/>
                    <rightPen lineWidth="0.5"/>
                </box>
                <textElement/>
                <textFieldExpression><![CDATA[$F{fruitsForSale}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

Can anybody advice where I maybe going wrong?

Alex K
  • 22,315
  • 19
  • 108
  • 236
raikumardipak
  • 1,461
  • 2
  • 29
  • 49
  • Did you get the result using *Java* code? – Alex K Feb 13 '14 at 11:11
  • @AlexK Alex, I am using Javabeans for the datasource. I modified the code to use a table with a dataset which refers to the static method prepareReport() in its datasourcexpression. But when the "fruitsForSale" does got displayed it gets as an array as explained above. I want it as tuples or individual records in a list. apparently though the datatype of the fruits name in the list is String it is not treating each element of the arraylist into a single record and showing it in a separate row in the list. I guess there is some basic stuff I am missing here. – raikumardipak Feb 14 '14 at 07:14
  • Possible duplicate of [JRBeanCollectionDataSource: How to show data from the java.util.List from JavaBean?](http://stackoverflow.com/questions/12209300/jrbeancollectiondatasource-how-to-show-data-from-the-java-util-list-from-javabe) – Alex K Jul 11 '16 at 21:47

0 Answers0