How can I generate a dynamic report using DynamicReports?

Harsha picture Harsha · Oct 28, 2011 · Viewed 7.6k times · Source

I have tried to generate a dynamic report using DynamicReports but always I am getting following exception,can anyone give me a solution.

Exception in thread "AWT-EventQueue-0" java.lang.NoSuchMethodError: net.sf.jasperreports.engine.base.JRBaseStyle.getParagraph()Lnet/sf/jasperreports/engine/JRParagraph;
    at net.sf.dynamicreports.jasper.transformation.StyleTransform.abstractStyle(StyleTransform.java:116)
    at net.sf.dynamicreports.jasper.transformation.StyleTransform.style(StyleTransform.java:74)
    at net.sf.dynamicreports.jasper.transformation.StyleTransform.addStyle(StyleTransform.java:65)
    at net.sf.dynamicreports.jasper.transformation.StyleTransform.transform(StyleTransform.java:59)
    at net.sf.dynamicreports.jasper.base.JasperReportDesign.transform(JasperReportDesign.java:104)
    at net.sf.dynamicreports.jasper.base.JasperReportDesign.<init>(JasperReportDesign.java:77)
    at net.sf.dynamicreports.jasper.base.JasperReportDesign.<init>(JasperReportDesign.java:69)
    at net.sf.dynamicreports.jasper.builder.JasperReportBuilder.toJasperReportDesign(JasperReportBuilder.java:215)
    at net.sf.dynamicreports.jasper.builder.JasperReportBuilder.getJasperParameters(JasperReportBuilder.java:242)
    at net.sf.dynamicreports.jasper.builder.JasperReportBuilder.toJasperPrint(JasperReportBuilder.java:252)
    at net.sf.dynamicreports.jasper.builder.JasperReportBuilder.show(JasperReportBuilder.java:281)
    at ClsManufac.ClsDynamicReport.build(ClsDynamicReport.java:43)
    at ClsManufac.ClsDynamicReport.<init>(ClsDynamicReport.java:30)
    at Manufacturing.FrmMonthlyStock.jButton1ActionPerformed(FrmMonthlyStock.java:335)
    at Manufacturing.FrmMonthlyStock.access$400(FrmMonthlyStock.java:23)
    at Manufacturing.FrmMonthlyStock$5.actionPerformed(FrmMonthlyStock.java:148)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2012)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2335)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:404)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:253)
    at java.awt.Component.processMouseEvent(Component.java:6175)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
    at java.awt.Component.processEvent(Component.java:5940)
    at java.awt.Container.processEvent(Container.java:2105)
    at java.awt.Component.dispatchEventImpl(Component.java:4536)
    at java.awt.Container.dispatchEventImpl(Container.java:2163)
    at java.awt.Component.dispatchEvent(Component.java:4362)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4461)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4125)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4055)
    at java.awt.Container.dispatchEventImpl(Container.java:2149)
    at java.awt.Window.dispatchEventImpl(Window.java:2478)
    at java.awt.Component.dispatchEvent(Component.java:4362)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:604)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:275)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:200)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:185)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:177)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:138)

I have a JTable and a JButton in my JFrame, what I need is to generate a report using DynamicReport (http://dynamicreports.sourceforge.net/) from the data of the JTable when click the button. What I do when clicking the button is, passing the table model to the following class, and try to loop through the table model to generate the report.

package com;


import java.awt.Color;

import java.awt.Font;
import static net.sf.dynamicreports.report.builder.DynamicReports.*;

import static net.sf.dynamicreports.report.builder.DynamicReports.*;

import java.math.BigDecimal;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import net.sf.dynamicreports.examples.DataSource;
import net.sf.dynamicreports.jasper.builder.JasperReportBuilder;
import net.sf.dynamicreports.report.builder.DynamicReports;
import net.sf.dynamicreports.report.builder.style.StyleBuilder;
import net.sf.dynamicreports.report.constant.HorizontalAlignment;
import net.sf.dynamicreports.report.constant.LineStyle;
import net.sf.dynamicreports.report.exception.DRException;
import net.sf.jasperreports.engine.JRDataSource;
import org.apache.commons.lang.Validate;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.collections.ReferenceMap;
import org.apache.commons.digester.Rule;

public class SimpleReport {

    DefaultTableModel model;

    public SimpleReport(DefaultTableModel model) {
        this.model = model;
        build();
    }

    private void build() {

        StyleBuilder boldStyle = stl.style().bold();
        StyleBuilder boldCenteredStyle = stl.style(boldStyle).setHorizontalAlignment(HorizontalAlignment.CENTER);
        StyleBuilder columnTitleStyle = stl.style(boldCenteredStyle);
        StyleBuilder titleStyle = stl.style(boldStyle).setHorizontalAlignment(HorizontalAlignment.CENTER);

        try {
            JasperReportBuilder report = DynamicReports.report();
            report()//create new report design
                    .setColumnTitleStyle(columnTitleStyle).columns(//add columns
                    //            title,     field name     data type
                    //col.columnRowNumberColumn("No").setHorizontalAlignment(HorizontalAlignment.CENTER),
                    col.column("ID", "ID", type.stringType()).setHorizontalAlignment(HorizontalAlignment.LEFT),
                    col.column("Name", "Name", type.stringType()).setHorizontalAlignment(HorizontalAlignment.CENTER),
                    col.column("Phone", "Phone", type.stringType()).setHorizontalAlignment(HorizontalAlignment.CENTER)).title(cmp.text("Getting started\n").setStyle(titleStyle))//shows report title
                    .pageFooter(cmp.pageXofY())//shows number of page at page footer
                    .setDataSource(createDataSource())//set datasource
                    .show();//create and show report
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private JRDataSource createDataSource() {
        DataSource dataSource = new DataSource("ID", "Name", "Phone");



        for (int i = 0; i < model.getRowCount(); i++) {
            System.out.println(model.getValueAt(i, 2));
            dataSource.add(model.getValueAt(i, 0), model.getValueAt(i, 1), model.getValueAt(i, 2));

        }

        return dataSource;
    }

}

Answer

JB Nizet picture JB Nizet · Oct 28, 2011

DynamicReports seems to use JasperReports, but you probably have incompatible versions of both libraries in your classpath.

Check the documentation of your version of DynamicReports to know which version of JasperReports it expects.