faelcavalcanti,
A idéia é justamente essa: Dentro de um parâmetro $P{texto} eu queria que o iReport usasse o campo $F{nome} para imprimir os nomes cadastrados no banco. tipo:
$P{texto} = “Caro senhor $F{nome}, viemos por meio desta blablabla”;
É esse $F{nome} dentro do parâmetro que eu queria que fosse reconhecido como um campo, e não como uma String. mas pelo visto não é possível fazer isso, certo?
A solução (que eu carinhosamente chamei de “gambi”) foi criar uma classe que gera todo o jrxml e seta a expressão do textfield com o texto que o usuário digita, e no meio eu coloco a expressão do campo “nome”.
Eis o método onde eu faço isso:
public void setCarta(String texto) {
texto = texto.replace("$nome", "\" + $F{nome} + \"");
texto = texto.replace("\n", "\\n");
texto = texto.replace("\r", "");
this.texto = texto;
this.carta = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\n" +
"<!-- Created with iReport - A designer for JasperReports -->\n" +
"<!DOCTYPE jasperReport PUBLIC \"//JasperReports//DTD Report Design//EN\" \"http://jasperreports.sourceforge.net/dtds/jasperreport.dtd\">\n" +
"<jasperReport\n" +
" name=\"carta\"\n" +
" columnCount=\"1\"\n" +
" printOrder=\"Vertical\"\n" +
" orientation=\"Portrait\"\n" +
" pageWidth=\"595\"\n" +
" pageHeight=\"842\"\n" +
" columnWidth=\"535\"\n" +
" columnSpacing=\"0\"\n" +
" leftMargin=\"30\"\n" +
" rightMargin=\"30\"\n" +
" topMargin=\"20\"\n" +
" bottomMargin=\"20\"\n" +
" whenNoDataType=\"NoPages\"\n" +
" isTitleNewPage=\"false\"\n" +
" isSummaryNewPage=\"false\">\n" +
" <property name=\"ireport.scriptlethandling\" value=\"0\" />\n" +
" <property name=\"ireport.encoding\" value=\"UTF-8\" />\n" +
" <import value=\"java.util.*\" />\n" +
" <import value=\"net.sf.jasperreports.engine.*\" />\n" +
" <import value=\"net.sf.jasperreports.engine.data.*\" />\n" +
"\n" +
" <parameter name=\"texto\" isForPrompting=\"true\" class=\"java.lang.String\"/>\n" +
" <parameter name=\"pathImagem\" isForPrompting=\"true\" class=\"java.lang.String\"/>\n" +
" <queryString><![CDATA[SELECT nome FROM associado]]></queryString>\n" +
"\n" +
" <field name=\"nome\" class=\"java.lang.String\"/>\n" +
"\n" +
" <background>\n" +
" <band height=\"0\" isSplitAllowed=\"true\" >\n" +
" </band>\n" +
" </background>\n" +
" <title>\n" +
" <band height=\"0\" isSplitAllowed=\"true\" >\n" +
" </band>\n" +
" </title>\n" +
" <pageHeader>\n" +
" <band height=\"129\" isSplitAllowed=\"true\" >\n" +
" <textField isStretchWithOverflow=\"false\" isBlankWhenNull=\"false\" evaluationTime=\"Now\" hyperlinkType=\"None\" hyperlinkTarget=\"Self\" >\n" +
" <reportElement\n" +
" x=\"0\"\n" +
" y=\"38\"\n" +
" width=\"406\"\n" +
" height=\"48\"\n" +
" key=\"textField\"/>\n" +
" <box></box>\n" +
" <textElement textAlignment=\"Center\">\n" +
" <font size=\"18\" isBold=\"true\"/>\n" +
" </textElement>\n" +
" <textFieldExpression class=\"java.lang.String\"><![CDATA[\"Afago - Associacao dos Aposentados e Pensionistas do Estado de Goias\"]]></textFieldExpression>\n" +
" </textField>\n" +
" <image evaluationTime=\"Now\" hyperlinkType=\"None\" hyperlinkTarget=\"Self\" >\n" +
" <reportElement\n" +
" x=\"406\"\n" +
" y=\"0\"\n" +
" width=\"128\"\n" +
" height=\"128\"\n" +
" key=\"image-1\"/>\n" +
" <box></box>\n" +
" <graphicElement stretchType=\"NoStretch\"/>\n" +
" <imageExpression class=\"java.lang.String\"><![CDATA[$P{pathImagem}]]></imageExpression>\n" +
" </image>\n" +
" </band>\n" +
" </pageHeader>\n" +
" <columnHeader>\n" +
" <band height=\"0\" isSplitAllowed=\"true\" >\n" +
" </band>\n" +
" </columnHeader>\n" +
" <detail>\n" +
" <band height=\"620\" isSplitAllowed=\"true\" >\n" +
" <textField isStretchWithOverflow=\"false\" isBlankWhenNull=\"false\" evaluationTime=\"Now\" hyperlinkType=\"None\" hyperlinkTarget=\"Self\" >\n" +
" <reportElement\n" +
" x=\"0\"\n" +
" y=\"0\"\n" +
" width=\"535\"\n" +
" height=\"620\"\n" +
" key=\"textField\"/>\n" +
" <box></box>\n" +
" <textElement textAlignment=\"Justified\">\n" +
" <font size=\"10\"/>\n" +
" </textElement>\n" +
" <textFieldExpression class=\"java.lang.String\"><![CDATA[\"" + texto + "\"]]></textFieldExpression>\n" + //AQUI É ONDE FAÇO A "GAMBI"
" </textField>\n" +
" </band>\n" +
" </detail>\n" +
" <columnFooter>\n" +
" <band height=\"0\" isSplitAllowed=\"true\" >\n" +
" </band>\n" +
" </columnFooter>\n" +
" <pageFooter>\n" +
" <band height=\"50\" isSplitAllowed=\"true\" >\n" +
" <staticText>\n" +
" <reportElement\n" +
" x=\"5\"\n" +
" y=\"3\"\n" +
" width=\"522\"\n" +
" height=\"36\"\n" +
" key=\"staticText-1\"/>\n" +
" <box></box>\n" +
" <textElement textAlignment=\"Center\">\n" +
" <font/>\n" +
" </textElement>\n" +
" <text><![CDATA[Rua 70, nro 523, St. Central - CEP 74055120 - Goiania - Goias\n" +
"Fone: ([telefone removido] ]]></text>\n" +
" </staticText>\n" +
" <line direction=\"TopDown\">\n" +
" <reportElement\n" +
" x=\"5\"\n" +
" y=\"0\"\n" +
" width=\"522\"\n" +
" height=\"0\"\n" +
" key=\"line-1\"/>\n" +
" <graphicElement stretchType=\"NoStretch\"/>\n" +
" </line>\n" +
" </band>\n" +
" </pageFooter>\n" +
" <summary>\n" +
" <band height=\"0\" isSplitAllowed=\"true\" >\n" +
" </band>\n" +
" </summary>\n" +
"</jasperReport>";
}
Foi a solução mais “amigável” que achei…