Sample2: First sample using the JasperReports API

In the first sample, we had seen a template based report (Sample 1), but in this second sample, we will design a report using only the JasperReports API.

In order to compile, fill, and export the report, we follow the same process as the one described in the sample 1, the difference is when loading the report, now, won’t load the report design from a template, we will create the report design using the classes that the JasperReports library provide us.

The “executeReport” method from “HelloWorldReportExecutor” class has the following changes:


ReportCreator reportCreator=new ReportCreator();
JasperDesign jasperDesign=reportCreator.createReport("Sample2Report");

In this case, we create an instance of “ReportCreator”, in this class we create the report design using the method “reportCreator.createReport”. Following the code:


JasperDesign jasperDesign=createBasicStructure(reportName);
for (JRDesignStyle jrDesignStyle : createStyles()) {
try {
jasperDesign.addStyle(jrDesignStyle);
} catch (JRException e) {
log.error("Error in adding a style to the report... "+e.getMessage());
}
}
jasperDesign.setTitle(
createTitleBand(
(JRStyle) jasperDesign.getStylesMap().get("titleStyle")
)
);
return(jasperDesign);

The basic report structure is created in the first lines (a call to the “createBasicStructure” method), then, the report styles are added, and finally, the title band is created and added.

This basic report structure implies the general elements in the report, e.g. page size, margins, report id, etc. In the template, this section should be the “jasperReport” tag. Following, the sample code:

private JasperDesign createBasicStructure(String reportName){
JasperDesign jasperDesign=new JasperDesign();
jasperDesign.setName(reportName);
jasperDesign.setColumnCount(1);
jasperDesign.setPageWidth(612);
jasperDesign.setPageHeight(792);
...
return(jasperDesign);
}

The “JasperDesign” class is a container for the report design, its attributes are the configurations of the report.

Next, we’ve to create the report styles and add it to the report design. Here is a sample of a style creation:

JRDesignStyle titleStyle=new JRDesignStyle();
titleStyle.setName("titleStyle");
titleStyle.setDefault(true);
titleStyle.setFontName("Helvetica");
titleStyle.setForecolor(Color.BLUE);
titleStyle.setFontSize(20);
titleStyle.setBold(true);
titleStyle.setItalic(false);
titleStyle.setUnderline(false);
titleStyle.setStrikeThrough(false);
titleStyle.setPdfFontName("Helvetica-Bold");
titleStyle.setPdfEncoding("Identity-H");
titleStyle.setPdfEmbedded(true);

We’ve created an instance of “JRDesignStyle” and set up the configurations for our report style. Later, this instance (what instance?) is added to the “JasperDesign” instance (as we’ve seen in the first sample code list).

Finally, the most time consumer task, we must create the title band. Here is the sample code:

private JRDesignBand createTitleBand(JRStyle jrStyle){
JRDesignBand designBand=new JRDesignBand();
designBand.setHeight(20);
designBand.setSplitAllowed(true);
//Create a TextField (textFieldTag attributes)
JRDesignTextField textField=new JRDesignTextField();
textField.setStretchWithOverflow(true);
textField.setBlankWhenNull(true);
textField.setEvaluationTime(JRExpression.EVALUATION_TIME_NOW);
textField.setHyperlinkType(JRHyperlink.HYPERLINK_TYPE_NONE);
textField.setHyperlinkTarget(JRHyperlink.HYPERLINK_TARGET_SELF);
textField.setStyle(jrStyle);
textField.setX(0);
textField.setY(4);
textField.setWidth(100);
textField.setHeight(13);
textField.setKey("titleTextField");
textField.setStretchType(JRElement.STRETCH_TYPE_RELATIVE_TO_TALLEST_OBJECT);
textField.setPositionType(JRElement.POSITION_TYPE_FLOAT);
//box tag properties
textField.getLineBox().setPadding(1);
textField.getLineBox().getPen().setLineWidth(0);
textField.getLineBox().getPen().setLineColor(Color.BLACK);
textField.getLineBox().getPen().setLineStyle(JRPen.LINE_STYLE_SOLID);
//TextField expression
JRDesignExpression designExpression=new JRDesignExpression();
designExpression.setValueClass(String.class);
designExpression.setText("\"Hello world, from JasperReports.... API\"");
//Setting the expression
textField.setExpression(designExpression);
//Add the textField to the band
designBand.addElement(textField);
return(designBand);
}

The first task, is to create the band using the “JRDesignBand” class, with this class instance, we can set some band attributes.

Next, we must create the text element using the “JRDesignTextField” class, with this class instance we can set the text field attributes e.g. size, position, style, etc. Here is something to emphasize, the textfield border and padding is configured using the “JRLineBox” class instance (this is accessed as property of “JRDesingTextField” instance).

Following, we’ve to create the text field expression, this is made creating an instance of the “JRDesignExpression” class. In this class we must set the expression type (“setValueClass” method) and the expression itself (“setText” method). Once all is set, this object should be aggregated to the “textField” object.

Once the text field is correctly instantiated, we must add it to the band, using the “addElement” method, and finally the title band is ready.

For a better understanding, I have added a class diagram that show the classes of the JasperReports API used in this sample.

Class diagram for sample 2

Well, here is the end of this sample, I hope this can be understood easily; in order to run the sample you must use the Ant script, or you can use your own java IDE.

The code of this sample can be downloaded here: Sample 2

3 comentarios el “Sample2: First sample using the JasperReports API

  1. Pingback: Second chapter of the JasperReports tutorial released! « Solo para burriquines

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s