#StackBounty: #java #jasper-reports #dynamic-jasper DynamicJasper – How to add a subreport as columns?

Bounty: 50


  • I have a Java class which has an ArrayList that must be printed to
    the jasperReport as a subreport.

  • I’m using DynamicJasper library.

  • The example on this question has been modified so it can be reproduced. However the data is different on the real case.


The current code Ive come with, prints the subreport in another line. Its ugly. I wanted the subreport to just be a group of columns that are concatenated.

To make it clear, here’s the current resultCurrent issue

And What I want is something like this: enter image description here

Current code

public class Test_subReport {

    protected static JasperPrint jp;
    protected static JasperReport jr;
    protected static Map params = new HashMap();
    protected static DynamicReport dr;

    public static void main(String args[]) throws SQLException, ColumnBuilderException, ClassNotFoundException {

        Test_subReport t = new Test_subReport();


    public void createReport() throws SQLException, ColumnBuilderException, ClassNotFoundException {

        ArrayList<Fruit> createMockDataset = createMockDataset();

        Style titleStyle = new Style();

        Style dataStyle = new Style();

        final List items = SortUtils.sortCollection(createMockDataset, Arrays.asList(new String[]{"name", "description"}));

        FastReportBuilder drb = new FastReportBuilder();
        drb.setTemplateFile("templatePortrait.jrxml", true, true, true, true);
        drb.addColumn("name", "name", String.class.getName(), 30, dataStyle)
                .addColumn("description", "description", String.class.getName(), 50, dataStyle)

        try {
            drb.addField("evaluations", List.class.getName());
            drb.addSubreportInGroupHeader(1, createSubreport("Evaluations"));
        } catch (Exception ex) {
            Logger.getLogger(Test_subReport.class.getName()).log(Level.SEVERE, null, ex);

        DynamicReport dynamicReport = drb.build();


        HashMap parametros = new HashMap();
        parametros.put("dataRelatorio", MyTools.getDataPorExtenso());

        doReport(dynamicReport, items, parametros);

    public void doReport(final DynamicReport _report, final Collection _data, HashMap parametros) {
        try {
            JRDataSource beanCollectionDataSource = new JRBeanCollectionDataSource(_data);

            JasperPrint jasperPrint = DynamicJasperHelper.generateJasperPrint(_report, new ClassicLayoutManager(), beanCollectionDataSource, parametros);

        } catch (JRException ex) {

    private DynamicReport createHeaderSubreport(String title) throws Exception {
        FastReportBuilder rb = new FastReportBuilder();
        DynamicReport dr = rb
                .addColumn("id", "id", Integer.class.getName(), 100)
                .addColumn("score", "score", Double.class.getName(), 50)
                .setMargins(5, 5, 20, 20)
        return dr;

    private Subreport createSubreport(String title) throws Exception {
        SubReportBuilder srb = new SubReportBuilder();
        srb.setDynamicReport(createHeaderSubreport(title), new ClassicLayoutManager())
                .setDataSource(DJConstants.DATA_SOURCE_ORIGIN_FIELD, DJConstants.DATA_SOURCE_TYPE_COLLECTION, "evaluations");
        return srb.build();

    public ArrayList<Fruit> createMockDataset() {
        ArrayList<Fruit> fruits = new ArrayList<>();

        Fruit f1 = new Fruit();
        f1.name = "Apple X1";
        f1.description = "Yummy yummy apple for the stackoverflow readers 1";

        Fruit f2 = new Fruit();
        f2.name = "Apple Ag";
        f2.description = "Yummy yummy apple for the stackoverflow readers 2";

        Fruit f3 = new Fruit();
        f3.name = "Apple Mn";
        f3.description = "Yummy yummy apple for the stackoverflow readers 3";

        Fruit f4 = new Fruit();
        f4.name = "Apple O2";
        f4.description = "Yummy yummy apple for the stackoverflow readers 4";

        //Evaluations for f1
        for (int i = 0; i < 4; i++) {
            Evaluation e = new Evaluation();
            e.id = i;
            e.score = Math.random() * 10;

        //evaluations for f4
        for (int i = 0; i < 4; i++) {
            Evaluation e = new Evaluation();
            e.id = i;
            e.score = Math.random() * 10;


        return fruits;


    public class Fruit {

        public String name;
        public String description;
        public ArrayList<Evaluation> evaluations = new ArrayList<Evaluation>();

        public Fruit() {


        public Fruit(String name, String description) {
            this.name = name;
            this.description = description;

        public String getName() {
            return name;

        public void setName(String name) {
            this.name = name;

        public String getDescription() {
            return description;

        public void setDescription(String description) {
            this.description = description;

        public ArrayList<Evaluation> getEvaluations() {
            return evaluations;

        public void setEvaluations(ArrayList<Evaluation> evaluations) {
            this.evaluations = evaluations;


    public class Evaluation {

        public int id;
        public double score;

        public Evaluation() {


        public Evaluation(int id, double score) {
            this.id = id;
            this.score = score;

        public int getId() {
            return id;

        public void setId(int id) {
            this.id = id;

        public double getScore() {
            return score;

        public void setScore(double score) {
            this.score = score;



Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.