title: How to create nested table like jasper report?
body: <p>I have been trying to create a jasper report which should look like the following image:</p>
  
  <p><a href="https://i.stack.imgur.com/hYneJ.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hYneJ.jpg" alt="desired output"></a></p>
  
  <p>For my sample report, <strong>Out-1</strong> and <strong>Out-2</strong> are rows for the outer table and <strong>asd</strong>, <strong>asds</strong>, <strong>adasd</strong> are rows for the inner table.</p>
  
  <p>I am trying to achieve it using nested tables. But the table takes the field and I can not assign field to the inner-table.</p>
  
  <p>I am creating jasper reports in Java code rather than using SQL connection to the table for some reason.</p>
  
  <h2>JXML</h2>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;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="report1" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="ec55a262-fec4-45f4-9b48-5be98088aafa"&gt;
      &lt;property name="ireport.zoom" value="1.0"/&gt;
      &lt;property name="ireport.x" value="0"/&gt;
      &lt;property name="ireport.y" value="0"/&gt;
      &lt;subDataset name="New Dataset 1" uuid="cdef25ec-abc4-45ba-b70e-28d82716626b"&gt;
          &lt;field name="sachNr" class="java.lang.String"/&gt;
          &lt;field name="akundenNr" class="java.lang.String"/&gt;
          &lt;field name="nestedTable" class="java.lang.String"/&gt;
      &lt;/subDataset&gt;
      &lt;subDataset name="New Dataset 2" uuid="eec983aa-a227-4a28-9c44-73cbe31fa024"&gt;
          &lt;field name="packStNr" class="java.lang.String"/&gt;
          &lt;field name="prodDatum" class="java.lang.String"/&gt;
          &lt;field name="stueck" class="java.lang.String"/&gt;
      &lt;/subDataset&gt;
      &lt;parameter name=" artikeldatenTable" class="java.lang.String"/&gt;
      &lt;background&gt;
          &lt;band splitType="Stretch"/&gt;
      &lt;/background&gt;
      &lt;title&gt;
          &lt;band height="79" splitType="Stretch"/&gt;
      &lt;/title&gt;
      &lt;pageHeader&gt;
          &lt;band height="35" splitType="Stretch"/&gt;
      &lt;/pageHeader&gt;
      &lt;columnHeader&gt;
          &lt;band height="61" splitType="Stretch"/&gt;
      &lt;/columnHeader&gt;
      &lt;detail&gt;
          &lt;band height="125" splitType="Stretch"&gt;
              &lt;componentElement&gt;
                  &lt;reportElement key="table" x="0" y="28" width="360" height="50" uuid="fae14075-18a8-4fcc-b6f9-c14e3623e75d"/&gt;
                  &lt;jr:table xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd"&gt;
                      &lt;datasetRun subDataset="New Dataset 1" uuid="17a10558-3a43-47c2-809d-6362924e5015"&gt;
                          &lt;dataSourceExpression&gt;&lt;![CDATA[new net.sf.jasperreports.engine.JREmptyDataSource(1)]]&gt;&lt;/dataSourceExpression&gt;
                      &lt;/datasetRun&gt;
                      &lt;jr:column width="137" uuid="7a8ca2ff-7a1a-41a0-8c30-b02f7be6e2b9"&gt;
                          &lt;jr:tableHeader height="30" rowSpan="1"&gt;
                              &lt;textField&gt;
                                  &lt;reportElement x="0" y="0" width="137" height="30" uuid="0fe0e502-dea8-45f6-9f96-633f5db90cd8"/&gt;
                                  &lt;textFieldExpression&gt;&lt;![CDATA[$F{sachNr}]]&gt;&lt;/textFieldExpression&gt;
                              &lt;/textField&gt;
                          &lt;/jr:tableHeader&gt;
                          &lt;jr:detailCell height="42" rowSpan="1"&gt;
                              &lt;componentElement&gt;
                                  &lt;reportElement key="table" x="0" y="0" width="137" height="42" uuid="9e72ca8c-182d-41c0-a021-98493162c7da"/&gt;
                                  &lt;jr:table&gt;
                                      &lt;datasetRun subDataset="New Dataset 2" uuid="7475e256-4bc5-4e88-be92-b6d2eb2fd4c5"&gt;
                                          &lt;dataSourceExpression&gt;&lt;![CDATA[new net.sf.jasperreports.engine.JREmptyDataSource()]]&gt;&lt;/dataSourceExpression&gt;
                                      &lt;/datasetRun&gt;
                                      &lt;jr:column width="90" uuid="cd851138-c894-459f-91c9-636c3602b247"&gt;
                                          &lt;jr:detailCell height="20" rowSpan="1"&gt;
                                              &lt;textField&gt;
                                                  &lt;reportElement x="0" y="0" width="90" height="20" uuid="ce21338e-c593-4bae-b7c4-08741127575c"/&gt;
                                                  &lt;textFieldExpression&gt;&lt;![CDATA[$F{packStNr}]]&gt;&lt;/textFieldExpression&gt;
                                              &lt;/textField&gt;
                                          &lt;/jr:detailCell&gt;
                                      &lt;/jr:column&gt;
                                      &lt;jr:column width="90" uuid="5f2b25a9-e34a-4698-b1ab-e8843dafec3c"&gt;
                                          &lt;jr:detailCell height="20" rowSpan="1"&gt;
                                              &lt;textField&gt;
                                                  &lt;reportElement x="0" y="0" width="90" height="20" uuid="5487305d-d22f-46fa-97bc-88e671f7085c"/&gt;
                                                  &lt;textFieldExpression&gt;&lt;![CDATA[$F{prodDatum}]]&gt;&lt;/textFieldExpression&gt;
                                              &lt;/textField&gt;
                                          &lt;/jr:detailCell&gt;
                                      &lt;/jr:column&gt;
                                      &lt;jr:column width="90" uuid="93c28f55-9bda-4be0-989a-03ce573715ba"&gt;
                                          &lt;jr:detailCell height="20" rowSpan="1"&gt;
                                              &lt;textField&gt;
                                                  &lt;reportElement x="0" y="0" width="90" height="20" uuid="53c4e2b3-7591-49b3-9b13-50cc1f16641a"/&gt;
                                                  &lt;textFieldExpression&gt;&lt;![CDATA[$F{stueck}]]&gt;&lt;/textFieldExpression&gt;
                                              &lt;/textField&gt;
                                          &lt;/jr:detailCell&gt;
                                      &lt;/jr:column&gt;
                                  &lt;/jr:table&gt;
                              &lt;/componentElement&gt;
                          &lt;/jr:detailCell&gt;
                      &lt;/jr:column&gt;
                      &lt;jr:column width="90" uuid="f898d2e7-1c2f-48fe-9357-61ee5d767a51"&gt;
                          &lt;jr:tableHeader height="30" rowSpan="1"&gt;
                              &lt;textField&gt;
                                  &lt;reportElement x="0" y="0" width="90" height="30" uuid="16b70095-2900-46f0-96ec-d638edb784f3"/&gt;
                                  &lt;textFieldExpression&gt;&lt;![CDATA[$F{akundenNr}]]&gt;&lt;/textFieldExpression&gt;
                              &lt;/textField&gt;
                          &lt;/jr:tableHeader&gt;
                          &lt;jr:detailCell height="42" rowSpan="1"/&gt;
                      &lt;/jr:column&gt;
                      &lt;jr:column width="111" uuid="0121d9c8-d1f0-447f-84ed-15385693a072"&gt;
                          &lt;jr:tableHeader height="30" rowSpan="1"/&gt;
                          &lt;jr:detailCell height="42" rowSpan="1"/&gt;
                      &lt;/jr:column&gt;
                      &lt;jr:column width="90" uuid="ca27e48c-a1d6-4c32-a1de-2580b6786b3d"&gt;
                          &lt;jr:tableHeader height="30" rowSpan="1"/&gt;
                          &lt;jr:detailCell height="42" rowSpan="1"/&gt;
                      &lt;/jr:column&gt;
                  &lt;/jr:table&gt;
              &lt;/componentElement&gt;
          &lt;/band&gt;
      &lt;/detail&gt;
      &lt;columnFooter&gt;
          &lt;band height="45" splitType="Stretch"/&gt;
      &lt;/columnFooter&gt;
      &lt;pageFooter&gt;
          &lt;band height="54" splitType="Stretch"/&gt;
      &lt;/pageFooter&gt;
      &lt;summary&gt;
          &lt;band height="42" splitType="Stretch"/&gt;
      &lt;/summary&gt;
  &lt;/jasperReport&gt;
  </code></pre>
  
  <h2>Outer Table POJO</h2>
  
  <pre><code>import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
  
  public class OuterTable {
  
      private String sachNr;
      private String akundenNr;
      private String SUBREPORT_DIR;
      private JRBeanCollectionDataSource nestedTable;
  
      public String getSachNr() {
          return sachNr;
      }
      public void setSachNr(String sachNr) {
          this.sachNr = sachNr;
      }
      public String getAkundenNr() {
          return akundenNr;
      }
      public void setAkundenNr(String akundenNr) {
          this.akundenNr = akundenNr;
      }
      public String getSUBREPORT_DIR() {
          return SUBREPORT_DIR;
      }
      public void setSUBREPORT_DIR(String sUBREPORT_DIR) {
          SUBREPORT_DIR = sUBREPORT_DIR;
      }
  
      public JRBeanCollectionDataSource getNestedTable() {
          return nestedTable;
      }
      public void setNestedTable(JRBeanCollectionDataSource nestedTable) {
          this.nestedTable = nestedTable;
      }
  
  }
  </code></pre>
  
  <h2>Inner Table POJO</h2>
  
  <pre><code>public class InnerTable {
  
      private String packStNr;
      private String prodDatum;
      private Integer stueck;
  
      public String getPackStNr() {
          return packStNr;
      }
      public void setPackStNr(String packStNr) {
          this.packStNr = packStNr;
      }
      public String getProdDatum() {
          return prodDatum;
      }
      public void setProdDatum(String prodDatum) {
          this.prodDatum = prodDatum;
      }
      public Integer getStueck() {
          return stueck;
      }
      public void setStueck(Integer stueck) {
          this.stueck = stueck;
      }
  
  }
  </code></pre>
  
  <h2>My Controller</h2>
  
  <pre><code>private void generateLagerbestand(List&lt;Artikeldaten&gt; artikelList) throws JRException, FileNotFoundException {
      JasperReport jasperReport;
  
      Map&lt;String, Object&gt; parameters = new HashMap&lt;String, Object&gt;();
  
      List &lt;OuterTable&gt; outerTableList = new ArrayList&lt;OuterTable&gt;();
      for(int i = 0; i &lt; artikelList.size(); i++) {
  
  
          List &lt;InnerTable&gt; innerTableList = new ArrayList&lt;InnerTable&gt;();
          List&lt;Lager&gt; lagersList = artikelList.get(i).getLagers();
          for(int j = 0; j &lt; lagersList.size(); j++) {
  
              InnerTable innerTable = new InnerTable();
  
              innerTable.setPackStNr(lagersList.get(j).getPackstnr());
              innerTable.setProdDatum(lagersList.get(j).getProddatum().toString());
              innerTable.setStueck(lagersList.get(j).getStueck());
  
              innerTableList.add(innerTable);
          }
  
          OuterTable outerTable = new OuterTable();
  
          outerTable.setAkundenNr(artikelList.get(i).getAkundennr());
          outerTable.setSachNr(artikelList.get(i).getSachnr());
  
          JRBeanCollectionDataSource nestedTable = new JRBeanCollectionDataSource(innerTableList);
          outerTable.setNestedTable(nestedTable);
  
          outerTableList.add(OuterTable);
      }
  
      try {
  
          jasperReport = JasperCompileManager.compileReport("PathToMyReport\\MyReport.jrxml");
          JRBeanCollectionDataSource outerTableItems = new JRBeanCollectionDataSource(outerTableList);
  
  
          // artikeldatenTable is outerTable
          parameters.put("artikeldatenTable", outerTableItems);
          JREmptyDataSource jrEmptyDataSource = new JREmptyDataSource();
          JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, jrEmptyDataSource);
          OutputStream outputStream = new FileOutputStream(new File("PathToMyPDF:\\MyPDF.pdf"));
  
          JasperExportManager.exportReportToPdfStream(jasperPrint, outputStream);
      } catch(Exception ex) {
          ex.printStackTrace();
      }
  }
  </code></pre>