I am creating Jasper object using JasperDesign class and initiating it using JRXmlLoader from jrxml file. I am extracting all the JRBands using getAllBands()
method to get all JRBands and from each band, I am extracting JRElements using method getElements()
of JRBand.
However, after getting each element like staticField
or textField
, I am not able to get their values from "TEXT" field which is private or protected.
How can I access these values?
You need to cast the JRElement
to the relative JRDesign class to be able to access the specific attributes of the element:
<textField>
to JRDesignTextField
<staticText>
to JRDesignStaticText
jrxml
<?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="testJasperDesign" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="597c0716-df6b-42ec-a7c8-863eb1b7174a">
<parameter name="testParam" class="java.lang.String">
<defaultValueExpression><![CDATA["Hello world"]]></defaultValueExpression>
</parameter>
<variable name="variable1" class="java.lang.String"/>
<title>
<band height="32" splitType="Stretch">
<textField>
<reportElement x="100" y="0" width="100" height="20" uuid="bf5a8f35-3faf-457b-a6fc-b29d97a9c332"/>
<textFieldExpression><![CDATA[$P{testParam}]]></textFieldExpression>
</textField>
<staticText>
<reportElement x="0" y="0" width="100" height="20" uuid="59922664-93a3-4f69-a906-5ff418d09cd3"/>
<text><![CDATA[Static text]]></text>
</staticText>
</band>
</title>
</jasperReport>
Java
public static void main(String[] args) throws JRException {
JasperDesign design = JRXmlLoader.load("jasper/testJasperDesign.jrxml");
JRBand titleBand = design.getTitle();
JRElement[] elements = titleBand.getElements();
for (JRElement element : elements) {
if (element instanceof JRDesignTextField){
JRDesignTextField textField = (JRDesignTextField) element;
JRExpression expression = textField.getExpression();
System.out.println(expression.getText());
}
if (element instanceof JRDesignStaticText){
JRDesignStaticText staticText = (JRDesignStaticText) element;
System.out.println(staticText.getText());
}
}
}
Output
$P{testParam}
Static text
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.