Getting Started

Try the examples first

The source code comes with examples ready to run (you need Maven 2 to build the source code). below a listing of some TestCases that are a good starting point.

Test & Description Sample PDF
A simple example that shows the basic setup of a report and its columns
A example that shows how to get more control on the basic setup of a report and its columns
Based on the PlainReportTest test, shows how to add some styles to the different elements in the report
Based on the StylesReportTest test, this test shows how to define groups and variables at the header and footer of each group
This test shows how to create a simple conditional style. The style of the values from a column changes as a certain condition is meat
This test shows how to add images to the title of the report and the header
Based on GroupsReportTest, shows how to add charts to a report, using a chart builder
Based on FastReportTest, shows how to add autotext out-of-the-box such as “page x/y”, etc
With almoust no code, you get an instant report! it uses the ReflectiveReportBuilder wich make some guesses upon the data type of the elements contained in the datasource and apply a style to them

Here is the online source code off the tests

Note: This tests extends junit.framework.TestCase, but they also contain a main(…) method, we recommend to execute the test through the main method because the JUnit framework closes the JasperViewer (a window that shows the report) inmeadiatly after exiting the program.

Creating a simple Report

This fragment shows how to create a report in an easy and fast way. see the code

FastReportBuilder drb = new FastReportBuilder();
DynamicReport dr = drb.addColumn("State", "state", String.class.getName(),30)
.addColumn("Branch", "branch", String.class.getName(),30)
.addColumn("Product Line", "productLine", String.class.getName(),50)
.addColumn("Item", "item", String.class.getName(),50)
.addColumn("Item Code", "id", Long.class.getName(),30,true)
.addColumn("Quantity", "quantity", Long.class.getName(),60,true)
.addColumn("Amount", "amount", Float.class.getName(),70,true)
.setTitle("November 2006 sales report")
.setSubtitle("This report was generated at " + new Date())

JRDataSource ds = new JRBeanCollectionDataSource(TestRepositoryProducts.getDummyCollection());
JasperPrint jp = DynamicJasperHelper.generateJasperPrint(dr, new ClassicLayoutManager(), ds);
JasperViewer.viewReport(jp);    //finally display the report report

This code produces the report shown below The FastReportBuilder provides the methods to pass the basic parameters to the report:

  • addColumn( <title><field/property to show><class name of the property to show><width of the column> )
  • addGroups (<number of columns to group by>)
  • setTitle(…) , setSubtitle( …) : guess!!!
  • setUseFullPageWidth( <boolean> ) : if true, the columns width will be resized proportionally until meet the page width

In the previous report, the DynamicJasper API makes a lot of decisions setting default values to the styles in general (title, column header, report detail, etc). This defaults are defined in theFastReportBuilder class. More control in all this aspects can be achieved using the DynamicReportBuilder class, as shown in the next example.

Getting more control on the report options

This code shows how to create a simple report but taking more control on the report options, the code is self explained. see the code

DynamicReportBuilder drb = new DynamicReportBuilder();
drb.setTitle("November 2006 sales report")		//defines the title of the report
        .setSubtitle("The items in this report correspond "
                        +"to the main products: DVDs, Books, Foods and Magazines")
        .setDetailHeight(15)		//defines the height for each record of the report
        .setMargins(30, 20, 30, 15)		//define the margin space for each side (top, bottom, left and right)
        .setDefaultStyles(titleStyle, subtitleStyle, headerStyle, detailStyle)
        .setColumnsPerPage(1);		//defines columns per page (like in the telephone guide)

 * Note that we still didn't call the build() method

 * Column definitions. We use a new ColumnBuilder instance for each
 * column, the ColumnBuilder.getNew() method returns a new instance
 * of the builder
AbstractColumn columnState = ColumnBuilder.getNew()		//creates a new instance of a ColumnBuilder
        .setColumnProperty("state", String.class.getName())		//defines the field of the data source that this column will show, also its type
        .setTitle("State")		//the title for the column
        .setWidth(85)		//the width of the column
        .build();		//builds and return a new AbstractColumn

//Create more columns
AbstractColumn columnBranch = ColumnBuilder.getNew()
        .setColumnProperty("branch", String.class.getName())

AbstractColumn columnaProductLine = ColumnBuilder.getNew()
        .setColumnProperty("productLine", String.class.getName())
        .setTitle("Product Line").setWidth(85)

AbstractColumn columnaItem = ColumnBuilder.getNew()
        .setColumnProperty("item", String.class.getName())

AbstractColumn columnCode = ColumnBuilder.getNew()
        .setColumnProperty("id", Long.class.getName())

AbstractColumn columnaCantidad = ColumnBuilder.getNew()
        .setColumnProperty("quantity", Long.class.getName())

AbstractColumn columnAmount = ColumnBuilder.getNew()
        .setColumnProperty("amount", Float.class.getName())
        .setPattern("$ 0.00")		//defines a pattern to apply to the values swhown (uses TextFormat)
        .setStyle(amountStyle)		//special style for this column (align right)

 * We add the columns to the report (through the builder) in the order
 * we want them to appear

 * add some more options to the report (through the builder)
drb.setUseFullPageWidth(true);  //we tell the report to use the full width of the page. this rezises
                                //the columns width proportionally to meat the page width.

DynamicReport dr =; //Finally build the report!

JRDataSource ds = new JRBeanCollectionDataSource(TestRepositoryProducts.getDummyCollection());    //Create a JRDataSource, the Collection used
                                                                                                //here contains dummy hardcoded objects...

JasperPrint jp = DynamicJasperHelper.generateJasperPrint(dr, new ClassicLayoutManager(), ds);    //Creates the JasperPrint object, we pass as a Parameter
                                                                                                //the DynamicReport, a new ClassicLayoutManager instance (this
                                                                                                //one does the magic) and the JRDataSource
JasperViewer.viewReport(jp);		//finally display the report report

The DynamicReportBuilder builds DynamicReport objects, this object contains the main options of the report (title, subtitle, styles, page size, orientation and a list of columns and groups) The ColumnBuilder builds SimpleColumn and ExpressionColumn objects depending the case (they both inherit from AbstractColumn). Each column holds basic data like the column title, the field from the datasource to show (it uses BeanUtils, so “a.b.c” can be passed as a property parameter), and how to show this data (style, pattern to apply, etc).

Note that the property_name passed to ColumnBuilder.setColumnProperty(“property_name”, “property_class_name”) method must match the name of a field/property in the elements contained in the datasource

Adding groups

Using the same code as before, we add this few lines. see the code

 GroupBuilder gb1 = new GroupBuilder();
 ColumnsGroup g1 = gb1.addCriteriaColumn((PropertyColumn) columnState).build();
 drb.addGroup(g1);              //add the group to the DynamicReportBuilder

Up to here, the DJ API will handle how to do the grouping stuff, but this isn’t enough for most of the users so lets see how to put some variables (totals) in the footer and header of the group.

GroupBuilder gb1 = new GroupBuilder();
        ColumnsGroup g1 = gb1.addCriteriaColumn((PropertyColumn) columnState)        //define the criteria column to group by (columnState)
            .addFooterVariable(columnAmount,ColumnsGroupVariableOperation.SUM)        //tell the group place a variable in the footer
                                                                                    //of the column "columnAmount" with the SUM of all
                                                                                    //values of the columnAmount in this group.

            .addFooterVariable(columnaQuantity,ColumnsGroupVariableOperation.SUM)    //idem for the columnaQuantity column
            .setGroupLayout(GroupLayout.DEFAULT_WITH_HEADER)                //tells the group how to be shown, there are many
                                                                                    //posibilities, see the GroupLayout class for more.

        GroupBuilder gb2 = new GroupBuilder();                                        //Create another group (using another column as criteria)
        ColumnsGroup g2 = gb2.setCriteriaColumn((PropertyColumn) columnBranch)        //and we add the same operations for the columnAmount and
            .addFooterVariable(columnAmount,ColumnsGroupVariableOperation.SUM)        //columnaQuantity columns

 //then we add the groups to the report (through the builder)

        drb.addGroup(g1);    //add group g1
        drb.addGroup(g2);    //add group g2

Here the order of the group registration is important and should be consistent with the order of the registration of the columns. If we mess here, the resultant report wont have any meaning.

Note: It’s important to remember that the data from the datasource must be ordered with the same criteria we want to group them.

Many more examples on groupsGrouping Styles

Adding charts

See examples here for the different char types

Fork me on GitHub