Jump to content
Changes to the Jaspersoft community edition download ×

Passing datasource into Subreport using Java


2004 IR Help

Recommended Posts

By: eqduffer - jonathan_b_king

Passing datasource into Subreport using Java

2005-10-06 16:35

 

Greetings all. I'm trying to set up a fairly simple program to demonstrate a subreport. It is using java to set up the report design and JRTableDataSource(s).

 

I've set up my parameters as so:

 

TableModel tm = new DefaultTableModel(values,columns);

JRDataSource ds = new JRTableModelDataSource;

JasperReport sub = JasperCompileManager.compileReport(getSubReportDesign("subreport"));

 

Map params = new HashMap();

params.put("sub1_ds",ds);

params.put("sub1_jr",sub);

 

And the subreport is defined using these parameters within expressions as:

 

JRDesignSubreport subReport = new JRDesignSubreport();

subReport.setUsingCache(false);

 

JRDesignExpression exp = new JRDesignExpression();

exp.setText("$P{sub1_jr}");

exp.setValueClass(net.sf.jasperreports.engine.JasperReport.class);

subReport.setExpression(exp);

 

exp = new JRDesignExpression();

exp.setText("$P{sub1_ds}");

exp.setValueClass(net.sf.jasperreports.engine.JRDataSource.class);

subReport.setDataSourceExpression(exp);

 

When I purely view the subreport it works, ala:

 

JasperPrint jp = JasperFillManager.fillReport(sub,null,ds);

JRViewer viewer = new JRViewer(jp);

 

But when I try and view the master, it does not:

 

JasperPrint jp = JasperFillManager.fillReport(jr,params,new JREmptyDataSource(1));

JRViewer viewer = new JRViewer(jp);

 

I'm not sure if it's the subreport expression or just the subreport datasource that isn't getting set properly, but I've been beating my head on this one all day and trying to get a good answer or find an example on the forums/google.

 

Any ideas? Full program source follows.

 

Thanks,

Jon

 

 

import net.sf.jasperreports.engine.*;

import net.sf.jasperreports.engine.data.JRTableModelDataSource;

import net.sf.jasperreports.engine.design.*;

import net.sf.jasperreports.view.JRViewer;

 

import javax.swing.*;

import javax.swing.table.DefaultTableModel;

import javax.swing.table.TableModel;

import java.util.HashMap;

import java.util.Map;

 

public class TestViewer

{

public static int DEFAULT_X_MARGIN = 30;

public static int DEFAULT_Y_MARGIN = 40;

private static int DEFAULT_PAGE_WIDTH = 600;

private static int DEFAULT_PAGE_HEIGHT = 810;

 

private static JRBand getTitleBand(String title)

{

JRDesignBand band = new JRDesignBand();

band.setHeight(50);

JRDesignLine line = new JRDesignLine();

line.setX(0);

line.setY(0);

line.setWidth(515);

line.setHeight(0);

band.addElement(line);

JRDesignStaticText textField = new JRDesignStaticText();

textField.setX(0);

textField.setY(10);

textField.setWidth(515);

textField.setHeight(30);

JRDesignReportFont bigFont = new JRDesignReportFont();

bigFont.setName("Arial_Normal");

bigFont.setDefault(true);

bigFont.setFontName("Arial");

bigFont.setSize(22);

bigFont.setPdfFontName("Helvetica");

bigFont.setPdfEncoding("Cp1252");

bigFont.setPdfEmbedded(false);

textField.setFont(bigFont);

textField.setText(title);

band.addElement(textField);

 

return band;

}

 

private static JRDesignBand getDetailBand()

{

//Detail

JRDesignBand band = new JRDesignBand();

band.setHeight(20);

JRDesignTextField textField = new JRDesignTextField();

textField.setX(0);

textField.setY(4);

textField.setWidth(50);

textField.setHeight(15);

JRDesignExpression expression = new JRDesignExpression();

expression.setValueClass(java.lang.String.class);

expression.setText("$F{one}");

textField.setExpression(expression);

band.addElement(textField);

textField = new JRDesignTextField();

textField.setStretchWithOverflow(true);

textField.setX(55);

textField.setY(4);

textField.setWidth(200);

textField.setHeight(15);

textField.setPositionType(JRElement.POSITION_TYPE_FLOAT);

expression = new JRDesignExpression();

expression.setValueClass(java.lang.String.class);

expression.setText("$F{two}");

textField.setExpression(expression);

band.addElement(textField);

 

return band;

}

 

private static JRDesignBand getEllipseBand()

{

JRDesignBand band = new JRDesignBand();

band.setHeight(200);

 

JRDesignEllipse ellipse = new JRDesignEllipse();

ellipse.setWidth(200);

ellipse.setHeight(200);

 

band.addElement(ellipse);

 

JRDesignExpression exp = new JRDesignExpression();

exp.setValueClass(java.lang.Boolean.class);

exp.setText("new Boolean(true)");

band.setPrintWhenExpression(exp);

 

return band;

}

 

private static JRDesignBand getSubReport()

{

JRDesignBand band = new JRDesignBand();

 

band.setHeight(400);

JRDesignSubreport subReport = new JRDesignSubreport();

subReport.setUsingCache(false);

 

JRDesignExpression exp = new JRDesignExpression();

exp.setText("$P{sub1_jr}");

exp.setValueClass(net.sf.jasperreports.engine.JasperReport.class);

subReport.setExpression(exp);

 

exp = new JRDesignExpression();

exp.setText("$P{sub1_ds}");

exp.setValueClass(net.sf.jasperreports.engine.JRDataSource.class);

subReport.setDataSourceExpression(exp);

 

band.addElement(subReport);

 

return band;

}

 

private static JasperDesign getSubReportDesign(String name) throws JRException

{

JasperDesign design = new JasperDesign();

design.setName(name);

design.setTitle(getTitleBand(name));

design.setPageWidth(DEFAULT_PAGE_WIDTH);

design.setPageHeight(DEFAULT_PAGE_HEIGHT);

 

JRDesignField field = new JRDesignField();

field.setName("one");

field.setValueClass(java.lang.String.class);

design.addField(field);

 

field = new JRDesignField();

field.setName("two");

field.setValueClass(java.lang.String.class);

design.addField(field);

 

field = new JRDesignField();

field.setName("three");

field.setValueClass(java.lang.String.class);

design.addField(field);

 

design.setSummary(getEllipseBand());

design.setDetail(getDetailBand());

 

return design;

}

 

private static JasperDesign getDesign() throws JRException {

JasperDesign design = new JasperDesign();

 

design.setName("TestReport");

design.setColumnCount(1);

design.setPageWidth(DEFAULT_PAGE_WIDTH);

design.setPageHeight(DEFAULT_PAGE_HEIGHT);

design.setTopMargin(DEFAULT_Y_MARGIN);

design.setBottomMargin(DEFAULT_Y_MARGIN);

 

design.setTitle(getTitleBand("Master Report"));

 

JRDesignParameter param = new JRDesignParameter();

param.setName("sub1_ds");

param.setValueClass(net.sf.jasperreports.engine.JRDataSource.class);

design.addParameter(param);

 

param.setName("sub1_jr");

param.setValueClass(net.sf.jasperreports.engine.JasperReport.class);

design.addParameter(param);

 

design.setDetail(getSubReport());

 

return design;

}

 

static String[] columns = {"one","two","three"};

static String[][] values = {{"1","2","3"},{"4","5","6"},{"7","8","9"}};

 

public static void main(String[] args)

{

try

{

JasperDesign design = getDesign();

JasperReport jr = JasperCompileManager.compileReport(design);

 

TableModel tm = new DefaultTableModel(values,columns);

JRDataSource ds = new JRTableModelDataSource;

JasperReport sub = JasperCompileManager.compileReport(getSubReportDesign("subreport"));

 

Map params = new HashMap();

params.put("sub1_ds",ds);

params.put("sub1_jr",sub);

 

// THIS WORKS! The subreport design works

// JasperPrint jp = JasperFillManager.fillReport(sub,null,ds);

// JRViewer viewer = new JRViewer(jp);

 

// THIS DOES NOT! The problem must be in passing the datasource down to the subreport

JasperPrint jp = JasperFillManager.fillReport(jr,params,new JREmptyDataSource(1));

JRViewer viewer = new JRViewer(jp);

 

 

JFrame frame = new JFrame();

frame.setSize(800,600);

frame.setContentPane(viewer);

frame.setVisible(true);

}

catch(Exception e)

{

e.printStackTrace();

}

}

}

 

 

 

 

 

By: Lucian Chirita - lucianc

RE: Passing datasource into Subreport using J

2005-10-07 00:24

Don't do this:

 

JRDesignParameter param = new JRDesignParameter();

param.setName("sub1_ds"); param.setValueClass(net.sf.jasperreports.engine.JRDataSource.class);

design.addParameter(param);

 

param.setName("sub1_jr"); param.setValueClass(net.sf.jasperreports.engine.JasperReport.class);

design.addParameter(param);

 

Reusing an object doesn't work because the JasperDesign doesn't create copies of passed objects. You need to create a new object for the second param.

 

HTH,

Lucian

 

 

 

 

By: Zoran Lazarevic - zoranl

RE: Passing datasource into Subreport using J

2005-10-07 01:05

Yep, you should create new instance for every param :

param = new JRDesignParameter();

 

Zoran

 

 

 

 

By: eqduffer - jonathan_b_king

RE: Passing datasource into Subreport using J

2005-10-07 09:37

 

Hah! That worked.

 

I must have read over those lines a hundred times yesterday, can't see the forest for the trees, I swore I was doing things right!

 

Thanks again,

Jon

Link to comment
Share on other sites

  • Replies 0
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...