Java Programming – IReports & JasperReports: Data Source: VECTOR

You would have to implement a custom data source and potentially a datasource provider, depending on if you would be filling the report programaticially or through the iReports GUI. Just be forewarned.. iReports has known bugs with custom data sources, it may be better to fill the Report programaticially. Alternatively, you can use JasperAssistant to design your reports.

If you had a vector then each element in the vector would be 1 iteration of the detail band in the report, and then you would have to access the “Fields” of the element returned from the vector in your custom data source. So if you had a vector of “Students” (name, student_num as simple fields) the data source would be (a very simple example)…

import java.util.Vector;

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRField;

public class TestDataSource implements JRDataSource {
private int m_nIdx;
private Vector<Student> v;

public TestDataSource() {
this (new Vector<Student>());

public TestDataSource(Vector<Student> v) {
m_nIdx = -1;
this.v = v;

public Object getFieldValue(JRField field) throws JRException {
Object o = null;

String sName = field.getName();

Student currStudent = v.elementAt(m_nIdx);

if (currStudent == null)
return null;

if (sName.equals("student_name"))
o = currStudent.getStudentName();
else if (sName.equals("student_number"))
o = currStudent.getStudentNum();

return o;

public boolean next() throws JRException {
return (m_nIdx < v.size());

You can use a Data source provider to describe to iReports what fields are available in your data source.

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRDataSourceProvider;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRField;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.JRField;

public class TestDataSourceProvider implements JRDataSourceProvider {

public TestDataSourceProvider() {


public JRDataSource create(JasperReport report) throws JRException {
TestDataSource ds = new TestDataSource(/**ADD_VECTOR_HERE_SOMEHOW*/);

return ds;

public void dispose(JRDataSource dataSource) throws JRException {
// nothing to dispose

public JRField[] getFields(JasperReport arg0) throws JRException, UnsupportedOperationException {
JRField[] fields = new JRField[2];
fields[0] = new JRBasicField("Student Name", "student_name", java.lang.String.class, "java.lang.String");
fields[1] = new JRBasicField("Student Number", "student_number", java.lang.String.class, "java.lang.String");

return fields;

public boolean supportsGetFieldsOperation() {
// TODO Auto-generated method stub
return true;


Based on the provider class above, we can see that your report would have 2 fields, $F{student_name} and $F{student_num}

Link tham khảo:


Post a Comment

Required fields are marked *


%d bloggers like this: