Iman Iman - 16 days ago 7
Java Question

Add custom data source to Jaspersoft Studio

I am trying to fill a table by passing a custom data source to it.
I have created a simple report with a table on it. The report it self gets the data from a ms sql database. I have written a java class similar to the class in this Example. But I get no value in table. At the example there is no scriptlet. I have checked the

(String) this.getFieldValue("KN_FormelGG");
line of code. It gets the data from field and can show it on report. So I guess the bean data source is not filled. I call the fill Table method in a
. How can I use Collection of data from java in jasper? I tried also adding the java bean in dataset and query dialog, but it did not help me either. Should I add the scriptlet to subreport/table? The main emphasis of my problem having the custom data source in a scriptlet. I went the other problem through, but I still get no answer. I added
to check the data, but it delivers null.

java class 1:

package testProjektIman.scriptlets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.jasperreports.engine.JRDefaultScriptlet;
import net.sf.jasperreports.engine.JRScriptletException;

public class FillTable extends JRDefaultScriptlet {

public void afterGroupInit(final String id)
throws JRScriptletException {
public ArrayList<String> splitGGArray(final String kNFormelGG) {
ArrayList<String> fieldNames = new ArrayList<>();
String[] array = (kNFormelGG.split(" "));
for (String sub : array) {
return fieldNames;

public Map<String, Object> fillTable()
throws JRScriptletException {
String kNFormelGG = null;
kNFormelGG = (String) this.getFieldValue("KN_FormelGG");
List<TableCells> listTableCells = new ArrayList<>();
TableCells tableCell;
for (String fn : splitGGArray(kNFormelGG)) {
tableCell = new TableCells();
JRBeanCollectionDataSource tableCellJRBean = new JRBeanCollectionDataSource(listTableCells);
Map<String, Object> parameters = new HashMap<>();
parameters.put("FieldDataSource", tableCellJRBean);
return parameters;


Java class 2

package testProjektIman.scriptlets;
public class TableCells {
private String fieldName;
private String keyFormel;
private String mK;
private String notation;
private String Item;
//getters setters


<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="" xmlns:xsi="" xsi:schemaLocation="" name="tableAutoFill" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="37fc3a9c-38e9-41be-9039-56249c5283d7">
<subDataset name="TableDataSource" uuid="5999e646-aeec-4b2b-b29b-68e897d56999">
<field name="fieldName" class="java.lang.String"/>
<scriptlet name="Filltable" class="testProjektIman.scriptlets.FillTable"/>
<parameter name="FieldDataSource" class="" isForPrompting="false"/>
<![CDATA[select * from "KennzahlReferenz2015_QIBericht", "Images"
where LB_ID = 62
and KN_OffiziellGruppe = 3
and IMG_ID = 1]]>
<field name="KN_Id" class="java.lang.Integer"/>
<field name="KN_FormelZ" class="java.lang.String"/>
<field name="KN_FormelGG" class="java.lang.String"/>
<group name="id">
<band height="50"/>
<band height="50"/>
<band height="191" splitType="Stretch">
<reportElement x="50" y="18" width="260" height="120" uuid="942ab836-df83-4a2f-8215-845073ad163f">
<property name="" value=""/>
<property name="" value="Table_TH"/>
<property name="" value="Table_CH"/>
<property name="" value="Table_TD"/>
<jr:table xmlns:jr="" xsi:schemaLocation="" whenNoDataType="AllSectionsNoDetail">
<datasetRun subDataset="TableDataSource" uuid="b554ada5-c388-4534-af8e-93571a417adb">
<jr:column width="100" uuid="f57e4e8e-8a2e-405f-b9ab-7a704e0986fd">
<property name="" value="Column1"/>
<jr:columnHeader style="Table_CH" height="30">
<reportElement x="0" y="0" width="100" height="30" uuid="9a55dd73-71e3-4559-9a8b-17d98bf17753"/>
<font isBold="true"/>
<jr:detailCell style="Table_TD" height="30">
<reportElement x="0" y="0" width="100" height="30" uuid="c3e6ccfc-9f91-4d7a-800a-613f5dded928"/>


Data Adapter

Create a data adapter file, for example adapter.xml, through the user interface. The contents might resemble:

<?xml version="1.0" encoding="UTF-8" ?>
<beanDataAdapter class="">

Data Class

Create a class that has a createCollection method, as per the data adapter definition above:

package com.compay.jasper;

public class JRDataSourceFactory {
     * @return A collection of data for the report.
    public static Collection<YourClass> createCollection() {
        return Arrays.asList( new YourClass() );

Set Report Properties

Ensure that the report has the following property (links the report to the custom data adapter):

<property name="" value="adapter.xml"/>

Set Report Fields

The report fields should now be able to reference the bean properties:

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

It's important that the fieldDescription element contain a value that reflects the bean property (i.e., the Java code you would normally call to retrieve the value from the bean on an instance of that bean).


The JRDataSourceFactory class stands alone -- it's used by the data adapter to create a collection of bean instances. The static method (createCollection) does the work here and does not, indeed cannot, use inheritance.


If possible, borrow the field names for the report from the bean's attributes. The code as written in the question makes it difficult to discern where the value for KN_Id comes from.

The following example links the data adapter to the field names in the report.

Bean Class

A bean exposes some properties:


public final class Student extends Entity {
    private String firstName;
    private String lastName;

    public String getFirstName() {
        return this.firstName;

    public String getLastName() {
        return this.lastName;

Data Adapter

The data adapter looks like:

<?xml version="1.0" encoding="UTF-8" ?>
<beanDataAdapter class="">

Data Class

The data class creates instances of the bean:

package com.compay.jasper;


public class JRDataSourceFactory {
    public static Collection<Student> createCollection() {
        return Arrays.asList( new Student() );

Report Fields

The report fields reflect the bean fields:

<field name="firstName" class="java.lang.String">
<field name="lastName" class="java.lang.String">

A collection of "student" instances is passed into the report. In this example, the collection contains a single instance. In your example, the collection could contain many instances. As the report library iterates over the collection, different values of firstName and lastName will be made available.

The mechanics for how the Student instance data is initially populated is outside the scope of this answer. As far as the reporting tool is concerned, it simply uses pre-populated instances of Student. If FillTable populates TableCells, that's not a concern for the reporting tool.