Jump to content
We've recently updated our Privacy Statement, available here ×

Displaying Java List's record In JasperReport


Recommended Posts

By: mpurswani - mpurswani

Displaying Java List's record In JasperReport

2006-04-07 08:39

Hi

 

I want to pass a list to JasperReport. Lets say

 

List lt = new ArrayList();

 

lt.add("1")

lt.add("2")

lt.add("3")

lt.add("5")

 

how will i display this in JasperReport in PDF format. what i have to add in the .jrxml file.

 

Thank you

 

 

 

 

 

 

 

By: coolcatt_ - coolcatt_

RE: Displaying Java List's record In JasperRe

2006-04-07 10:55

JasperDesign jasperDesign = JRXmlLoader.load("c:\Test.jrxml");

JasperReport jr = JasperCompileManager.compileReport(jasperDesign);

Map parameters = new HashMap();

JasperPrint jasperPrint = null;

 

Vector lt = new Vector();

 

lt.add(getHashTable("1"));

lt.add(getHashTable("2"));

lt.add(getHashTable("3"));

lt.add(getHashTable("5"));

 

getHashTable(String s){

Hashtable h = new Hashtable();

h.put("number",s);

return h;

}

JRMapCollectionDataSource mc = new JRMapCollectionDataSource(lt);

jasperPrint = JasperFillManager.fillReport(jr, parameters, mc);

JasperExportManager.exportReportToPdfFile(jasperPrint,"c:\test.pdf");

 

in your jrxml file you need a field like this,

<field name="number" class="java.lang.String"/>

 

hope this helps, this worked for me. I just used a vector instead of list, and a hashtable for my collection of fields. To add more fields, just add more to your report and name the hashtable key the same as your field.

 

Cal.

 

 

 

 

By: mpurswani - mpurswani

RE: Displaying Java List's record In JasperReport

2006-04-08 05:14

Thank you your reply.

 

i did what you had suggested for diaplaying data using Vactor. Now what i am doing is

 

JasperDesign jasperDesign = JRXmlLoader.load("c:\Test.jrxml");

 

JasperReport jr = JasperCompileManager.compileReport(jasperDesign);

 

Map parameters = new HashMap();

JasperPrint jasperPrint = null;

Vector lt = new Vector();

 

lt.add(getHashTable("1"));

lt.add(getHashTable("2"));

lt.add(getHashTable("3"));

lt.add(getHashTable("5"));

 

 

Hashtable getHashTable(String s){

Hashtable h = new Hashtable();

h.put("number",s);

return h;

}

 

JRMapCollectionDataSource mc = new JRMapCollectionDataSource(lt);

jasperPrint = JasperFillManager.fillReport(jr, parameters, mc);

 

JasperExportManager.exportReportToPdfFile(jasperPrint,"c:\test.pdf");

JasperViewer.viewReport(jasperPrint);

 

 

 

I made the changes in test.jrxml file also. now it is like

 

<?xml version="1.0"?>

<!DOCTYPE jasperReport

PUBLIC "-//JasperReports//DTD Report Design//EN"

"http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">

 

<jasperReport name="Simple_Report">

<field name="number" class="java.lang.String"/>

</jasperReport>

 

 

 

but it is still not showing data in pdf file although it is creating PDF file. i checked the values in Vactor also, vactor has the values.

 

Could u please tell me what is wrong, why i am not getting data in pdf file. pls help me it is very urgent.

 

thank you

manish.purswani@gmail.com

 

 

 

 

 

 

By: willibald - willibald

RE: Displaying Java List's record In JasperRe

2006-04-10 02:45

Hi,

 

I have the same simpel question!!

 

when I use a Database-Query "select * from .." I get all the values iterated. But when I would like to define a Collection in my Java-programm and pass this to the Report. I check this here out, but it only displayed 'null, 1' in the report.

 

There must be a very very simple solution.

 

Thanks for any help.

 

 

 

 

 

By: takada - regittakada

RE: Displaying Java List's record In JasperRe

2006-04-10 04:52

@mpurswani:

 

your class "JRMapCollectionDataSource" have to implement "JRDataSource"...there are two methods which are used.

 

public boolean next() throws JRException - iterates your data

public Object getFieldValue(JRField field) throws JRException -- deliver the data to the fields in your report.

 

both of this methods you have to implement and define

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...