René Nyffenegger's collection of things on the web
René Nyffenegger on Oracle - Most wanted - Feedback -
 

Jasper - Patterns

Patterns are used to format values in the report.

First example

Putting the minus character for negative numbers on the number's right side rather than its left side.
Note how F{} of COL_3 is placed one character to the right with regard to its column title to make the appearance of a vertically alligned column.
Note also how the 4th column is not correctly aligned at all.
Note further that the txt-export is completely messed.
<?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         ='Patterns' 
 pageWidth    =     '560' 
 pageHeight   =     '420' 
 columnWidth  =     '560' 
 leftMargin   =       '0' 
 rightMargin  =       '0' 
 topMargin    =       '0' 
 bottomMargin =       '0'
>
  <queryString>
    <![CDATA[

    select      5.2   col_1,    -18.92  col_2,      65.48  col_3,  123456789.11 col_4 from dual union all
    select     -5.2   col_1,     18.92  col_2,     -39.1   col_3, -123456789.11 col_4 from dual union all
    select     -7.111 col_1,    200.002 col_2,   -1021.765 col_3,   23456789.11 col_4 from dual union all
    select   1024.0   col_1,  -9999.99  col_2,    5873.911 col_3, - 23456789.11 col_4 from dual union all
    select  12345.67  col_1,  89101.11  col_2,   21314.156 col_3,    3456789.11 col_4 from dual union all
    select -17181.92  col_1,-202122.23  col_2, -242526.272 col_3, -  3456789.11 col_4 from dual

  ]]>

  </queryString>
  <field name='COL_1' class='java.math.BigDecimal'/>
  <field name='COL_2' class='java.math.BigDecimal'/>
  <field name='COL_3' class='java.math.BigDecimal'/>
  <field name='COL_4' class='java.math.BigDecimal'/>

  <title>
    <band height='28'>
      <staticText><reportElement x= '14' y='0' width='126' height='14'/><textElement textAlignment='Right'><font fontName='Courier New' size='12'/></textElement><text><![CDATA[COL_1]]></text></staticText>
      <staticText><reportElement x='140' y='0' width='126' height='14'/><textElement textAlignment='Right'><font fontName='Courier New' size='12'/></textElement><text><![CDATA[COL_2]]></text></staticText>
      <staticText><reportElement x='266' y='0' width='126' height='14'/><textElement textAlignment='Right'><font fontName='Courier New' size='12'/></textElement><text><![CDATA[COL_3]]></text></staticText>
      <staticText><reportElement x='420' y='0' width='126' height='14'/><textElement textAlignment='Right'><font fontName='Courier New' size='12'/></textElement><text><![CDATA[COL_4]]></text></staticText>
    </band>
  </title>

  <detail>
    <band height='14'>
      <textField>                                          <reportElement x= '14' y='0' width='126' height='14'/> <textElement textAlignment='Right'> <font fontName='Courier New' size='12'/> </textElement> <textFieldExpression class='java.math.BigDecimal'><![CDATA[$F{COL_1}]]> </textFieldExpression> </textField>
      <textField pattern='###,###,##0.00 ;###,###,##0.00-'><reportElement x='140' y='0' width='126' height='14'/> <textElement textAlignment='Right'> <font fontName='Courier New' size='12'/> </textElement> <textFieldExpression class='java.math.BigDecimal'><![CDATA[$F{COL_2}]]> </textFieldExpression> </textField>
      <textField pattern='###,###,##0.00 ;###,###,##0.00-'><reportElement x='273' y='0' width='126' height='14'/> <textElement textAlignment='Right'> <font fontName='Courier New' size='12'/> </textElement> <textFieldExpression class='java.math.BigDecimal'><![CDATA[$F{COL_3}]]> </textFieldExpression> </textField>
      <textField pattern='###,###,##0.00 ;###,###,##0.00-'><reportElement x='420' y='0' width= '98' height='14'/> <textElement textAlignment='Right'> <font fontName='Courier New' size='12'/> </textElement> <textFieldExpression class='java.math.BigDecimal'><![CDATA[$F{COL_4}]]> </textFieldExpression> </textField>
    </band>
  </detail>
</jasperReport>

Second example

Minus at the beginning of a number.
<?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              ="Demonstrating Subreports" 
  pageWidth         =        "595" 
  pageHeight        =        "845"
  columnWidth       =        "595" 
  leftMargin        =          "0" 
  rightMargin       =          "0" 
  topMargin         =          "0" 
  bottomMargin      =          "0"
>


  <queryString>
    <![CDATA[
    
  --------------

    select    5.2   col_1,   -18.92  col_2 from dual union all
    select   -5.2   col_1,    18.92  col_2 from dual union all
    select   -7.111 col_1,   200.002 col_2 from dual union all
    select 1024.0   col_1, -9999.99  col_2 from dual

  --------------
]]>
  </queryString>

  <field name="COL_1" class="java.lang.Float"/>
  <field name="COL_2" class="java.lang.Float"/>


  <detail>
    <band height="28" splitType="Stretch">

      <staticText>                  <reportElement x="20" y= "0" width="60" height="14"/> <textElement><font fontName="Courier New" size="12"/></textElement> <text><![CDATA[COL_1]]>                                            </text>                </staticText>
      <textField pattern="-###0.00"><reportElement x="20" y="14" width="60" height="14"/> <textElement><font fontName="Courier New" size="12"/></textElement> <textFieldExpression class="java.lang.Float"><![CDATA[$F{COL_1}]]> </textFieldExpression> </textField>
      <staticText>                  <reportElement x="90" y= "0" width="60" height="14"/> <textElement><font fontName="Courier New" size="12"/></textElement> <text><![CDATA[COL_2]]>                                            </text>                </staticText>
      <textField pattern= "###0.00"><reportElement x="90" y="14" width="60" height="14"/> <textElement><font fontName="Courier New" size="12"/></textElement> <textFieldExpression class="java.lang.Float"><![CDATA[$F{COL_2}]]> </textFieldExpression> </textField>

    </band>
  </detail>

</jasperReport>

Third example

Figures starting with a dot in case there in the range [0,1[.
<?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="Demonstrating Subreports" 
  pageWidth="595" 
  pageHeight="845" 
  columnWidth="595" 
  leftMargin="0" 
  rightMargin="0" 
  topMargin="0" 
  bottomMargin="0"

>
	<queryString>

		<![CDATA[

    select    5.2   col_1,   -18.92   col_2 from dual union all
    select   -5.2   col_1,    18.92   col_2 from dual union all
    select   -7.111 col_1,   200.002  col_2 from dual union all
    select 1024.0   col_1, -9999.99   col_2 from dual union all
    select    0     col_1,     0      col_2 from dual union all
    select     .9   col_1,    - .8    col_2 from dual union all
    select     .8   col_1,      .01   col_2 from dual union all
    select     .7   col_1,      .002  col_2 from dual union all
    select     .6   col_1,      .0003 col_2 from dual

  ]]>

	</queryString>
	<field name="COL_1" class="java.lang.Float"/>
	<field name="COL_2" class="java.lang.Float"/>

  <pageHeader>
    <band height='28'>
			<staticText> <reportElement x='20' y='0' width='60' height='14'/> <textElement> <font fontName='Courier New' size='12'/> </textElement> <text><![CDATA[COL_1]]></text> </staticText> 
      <staticText> <reportElement x='90' y='0' width='60' height='14'/> <textElement> <font fontName='Courier New' size='12'/> </textElement> <text><![CDATA[COL_2]]></text> </staticText>
    </band>
  </pageHeader>


	<detail>
		<band height='14'>
			<textField>                <reportElement x='20' y='0' width='60' height='14'/><textElement><font fontName='Courier New' size='12'/></textElement> <textFieldExpression class='java.lang.Float'><![CDATA[$F{COL_1}]]></textFieldExpression></textField>
			<textField pattern='.0000'><reportElement x='90' y='0' width='60' height='14'/><textElement><font fontName='Courier New' size='12'/></textElement> <textFieldExpression class='java.lang.Float'><![CDATA[$F{COL_2}]]></textFieldExpression></textField> 
    </band>
	</detail>
</jasperReport>