title: How to create a dynamic report thorough jrxml?
body: <p>I am working on <em>jrxml</em> to create dynamic reports. I have parameterized the columns i.e. the <em>jrxml</em> for that report can be used to generate other reports as well. </p>
  
  <p>However, I have not managed to make the fields flexible. That is, if the user selects 4 columns it would work but if 1 or 2 or 3 columns are selected, it gives an error since the field names are unidentified. </p>
  
  <p>Please post a solution urgently if something like a default expression for fieldname can be created or a for loop/java script can be used.</p>
  
  <p>Moreover, how can jasper designer be exactly used to achieve this? </p>
  
  <p>The jrxml is as follows:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0"?&gt;
  &lt;!DOCTYPE jasperReport
    PUBLIC "-//JasperReports//DTD Report Design//EN"
    "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd"&gt;
  &lt;jasperReport name="report1"&gt;
      &lt;parameter name="reportTitle" class="java.lang.String"/&gt;
      &lt;parameter name="author" class="java.lang.String"/&gt;
      &lt;parameter name="startDate" class="java.lang.String"/&gt;
  
      &lt;parameter name="C1" class="java.lang.String"&gt;
          &lt;defaultValueExpression&gt;
              new java.lang.String("")
          &lt;/defaultValueExpression&gt;
      &lt;/parameter&gt;
      &lt;parameter name="C2" class="java.lang.String"&gt;
          &lt;defaultValueExpression&gt;
              new java.lang.String("")
          &lt;/defaultValueExpression&gt;
      &lt;/parameter&gt;
      &lt;parameter name="C3" class="java.lang.String"&gt;
          &lt;defaultValueExpression&gt;
              new java.lang.String("")
          &lt;/defaultValueExpression&gt;
      &lt;/parameter&gt;
      &lt;parameter name="C4" class="java.lang.String"&gt;
          &lt;defaultValueExpression&gt;
              new java.lang.String("default parameter value")
          &lt;/defaultValueExpression&gt;
      &lt;/parameter&gt;
  
      &lt;field name="COLUMN_1" class="java.lang.Integer"/&gt;
      &lt;field name="COLUMN_2" class="java.lang.Integer"/&gt;
      &lt;field name="COLUMN_3" class="java.lang.Integer"/&gt;
      &lt;field name="COLUMN_4" class="java.lang.Integer"/&gt;
  
      &lt;title&gt;
          &lt;band height="60"&gt;
              &lt;textField&gt;
                  &lt;reportElement x="0" y="10" width="500" height="40"/&gt;
                  &lt;textElement textAlignment="Center"&gt;
                      &lt;font size="24"/&gt;
                  &lt;/textElement&gt;
                  &lt;textFieldExpression class="java.lang.String"&gt;
                      &lt;![CDATA[$P{reportTitle}]]&gt;
                  &lt;/textFieldExpression&gt;
              &lt;/textField&gt;
              &lt;textField&gt;
                  &lt;reportElement x="0" y="40" width="500" height="20"/&gt;
                  &lt;textElement textAlignment="Center"/&gt;
                  &lt;textFieldExpression class="java.lang.String"&gt;
                      &lt;![CDATA["Run by: " + $P{author}
                          + " on " + $P{startDate}]]&gt;
                  &lt;/textFieldExpression&gt;
              &lt;/textField&gt;
          &lt;/band&gt;
      &lt;/title&gt;
  
      &lt;columnHeader&gt;
          &lt;band height="30"&gt;
              &lt;rectangle&gt;
                  &lt;reportElement x="0" y="0" width="500" height="25"/&gt;
                  &lt;graphicElement/&gt;
              &lt;/rectangle&gt;
  
              &lt;textField&gt;
                  &lt;reportElement x="0" y="5" width="170" height="15"/&gt;
                  &lt;textFieldExpression class="java.lang.String"&gt;
                      &lt;![CDATA[$P{C1}]]&gt;
                  &lt;/textFieldExpression&gt;
              &lt;/textField&gt;
  
              &lt;textField&gt;
                  &lt;reportElement x="70" y="5" width="170" height="15"/&gt;
                  &lt;textFieldExpression class="java.lang.String"&gt;
                      &lt;![CDATA[$P{C2}]]&gt;
                  &lt;/textFieldExpression&gt;
              &lt;/textField&gt;
  
              &lt;textField&gt;
                  &lt;reportElement x="150" y="5" width="150" height="15"/&gt;
                  &lt;textFieldExpression class="java.lang.String"&gt;
                      &lt;![CDATA[$P{C3}]]&gt;
                  &lt;/textFieldExpression&gt;
              &lt;/textField&gt;
  
              &lt;textField&gt;
                  &lt;reportElement x="300" y="5" width="150" height="15"/&gt;
                  &lt;textFieldExpression class="java.lang.String"&gt;
                      &lt;![CDATA[$P{C4}]]&gt;
                  &lt;/textFieldExpression&gt;
              &lt;/textField&gt;
          &lt;/band&gt;
      &lt;/columnHeader&gt;
  
      &lt;detail&gt;
          &lt;band height="20"&gt;
              &lt;textField&gt;
                  &lt;reportElement x="5" y="0" width="50" height="15"/&gt;
                  &lt;textElement/&gt;
                  &lt;textFieldExpression class="java.lang.Integer"&gt;
                      &lt;![CDATA[$F{COLUMN_1}]]&gt;
                  &lt;/textFieldExpression&gt;
              &lt;/textField&gt;
  
              &lt;textField&gt;
                  &lt;reportElement x="90" y="0" width="150" height="15"/&gt;
                  &lt;textElement/&gt;
                  &lt;textFieldExpression class="java.lang.Integer"&gt;
                      &lt;![CDATA[$F{COLUMN_2}]]&gt;
                  &lt;/textFieldExpression&gt;
              &lt;/textField&gt;
  
              &lt;textField&gt;
                  &lt;reportElement x="170" y="0" width="50" height="15"/&gt;
                  &lt;textElement/&gt;
                  &lt;textFieldExpression class="java.lang.Integer"&gt;
                      &lt;![CDATA[$F{COLUMN_3}]]&gt;
                  &lt;/textFieldExpression&gt;
              &lt;/textField&gt;
  
              &lt;textField&gt;
                  &lt;reportElement x="320" y="0" width="150" height="15"/&gt;
                  &lt;textElement/&gt;
                  &lt;textFieldExpression class="java.lang.Integer"&gt;
                      &lt;![CDATA[$F{COLUMN_4}]]&gt;
                  &lt;/textFieldExpression&gt;
              &lt;/textField&gt;
          &lt;/band&gt;
      &lt;/detail&gt;
  &lt;/jasperReport&gt;
  </code></pre>