I faced some problem while working in last project. I want to share some tricks which I used in jasper report and in dynamic jasper.
1. Add combination of string and parameter/field value in textfield dynamically. Normally we put in expression either field/parameter value or string. If we are going to set String with field/parameter value then its little bit tricky. It always create trouble for me while try to insert expression.
Here is a scenario how we generate jasper design.
public JasperDesign createDesign() throws JRException { JasperDesign jasperDesign = new JasperDesign(); /*Set basic design of page.*/ jasperDesign.setName("sampleDynamicJasperDesign"); jasperDesign.setPageWidth(595); // page width jasperDesign.setPageHeight(842); // page height jasperDesign.setColumnWidth(515); // column width of page jasperDesign.setColumnSpacing(0); jasperDesign.setLeftMargin(40); jasperDesign.setRightMargin(40); jasperDesign.setTopMargin(20); jasperDesign.setBottomMargin(20); JRDesignExpression expression = new JRDesignExpression(); //Set style of page. JRDesignStyle normalStyle = new JRDesignStyle(); normalStyle.setName("Sans_Normal"); normalStyle.setDefault(true); normalStyle.setFontName("DejaVu Sans"); normalStyle.setFontSize(12); normalStyle.setPdfFontName("Helvetica"); normalStyle.setPdfEncoding("Cp1252"); normalStyle.setPdfEmbedded(false); jasperDesign.addStyle(normalStyle); /* * Generate field dynamically * */ JRDesignField field = new JRDesignField(); field.setName("firstName"); field.setValueClass(String.class); jasperDesign.addField(field); field = new JRDesignField(); field.setName("lastName"); // set name for field. field.setValueClass(String.class); // set class for field. Its always depends upon data type which we want to get in this field. jasperDesign.addField(field); // Added field in design. field = new JRDesignField(); field.setName("age"); field.setValueClass(Integer.class); jasperDesign.addField(field); JRDesignBand band = new JRDesignBand(); //Title Band band = new JRDesignBand(); band.setHeight(30); JRDesignStaticText staticText = new JRDesignStaticText(); staticText.setText("Person's Specification"); staticText.setX(0); staticText.setY(0); staticText.setHeight(20); staticText.setWidth(515); staticText.setHorizontalAlignment(HorizontalAlignEnum.CENTER); band.addElement(staticText); jasperDesign.setTitle(band); // Detail Band band = new JRDesignBand(); // New band band.setHeight(20); // Set band height /*Create text field dynamically*/ JRDesignTextField textField = new JRDesignTextField(); textField.setX(0); // x position of text field. textField.setY(0); // y position of text field. textField.setWidth(160); // set width of text field. textField.setHeight(20); // set height of text field. JRDesignExpression jrExpression = new JRDesignExpression(); // new instanse of expression. We need create new instance always when need to set expression. jrExpression.setText(""" + "First Name: " + """ + "+" + "$F{firstName}"); // Added String before field in expression. textField.setExpression(jrExpression); // set expression value in textfield. band.addElement(textField); // Added element in textfield. textField = new JRDesignTextField(); textField.setX(160); textField.setY(0); textField.setWidth(160); textField.setHeight(20); jrExpression = new JRDesignExpression(); jrExpression.setText("$F{lastName}" + "+" + """ + " :Last Name" + """); // Added string after field value textField.setExpression(jrExpression); band.addElement(textField); textField = new JRDesignTextField(); textField.setX(320); textField.setY(0); textField.setWidth(160); textField.setHeight(20); jrExpression = new JRDesignExpression(); String age = """ + "<font>" + """ + "+" + """ + "Age is: " + """ + "+" + """ + "</font><font>" + """ + "+" + "$F{age}" + "+" + """ + "</font>" + """; // added html in text field with different color. jrExpression.setText(age); textField.setExpression(jrExpression); textField.setMarkup("html"); // By Default markup is none, We need to set it as html if we set expression as html. band.addElement(textField); ((JRDesignSection) jasperDesign.getDetailSection()).addBand(band); return jasperDesign; }
Here is explanation how we insert value in textfield expression.
First scenario for String with field.
JRDesignExpression jrExpression = new JRDesignExpression(); // new instanse of expression. We need create new instance always when need to set expression. jrExpression.setText(""" + "First Name: " + """ + "+" + "$F{firstName}"); // Added String
before field in expression.
When we write expression then at the end of the day It looks like this in reportExpression:
"First Name: " + $F{firstName}
Thats makes sense for textfield expression.
Second scenario:
String age = """ + "<font>" + """ + "+" + """ + "Age is: " + """ + "+" + """ + "</font><font>" + """ + "+" + "$F{age}" + "+" + """ + "</font>" + """; // added html in text field with different color. jrExpression.setText(age);
It will show in textfieldExpression:
"<font color="#66FF33">"+"Age is: "+"</font><font color="#6600FF">"+$F{age}+"</font>"
Hope it will help you to add expression in text field.
Recent Comments