Skip to content
Sandny Blog
  • Home
  • Java
  • JavaScript
  • AWS
  • Embedded
  • About
Jasper

Jasper Subreporting for a CV generator

  • May 16, 2017May 20, 2017
  • by Coder Kai

Hi everyone,

This is a tutorial for Jasper Subreporting with dataresource for beginners. My project is going to be a single page with details of a CV. It will look like below

Figure 1: What CV will look like
Figure 1: What CV will look like

Before all these, the structure of data resources in parameters of the jasper report is going to be as follow

Figure 2: Structure of Data Sources

I’m not using  any data models or entities to pass these data, so making model classes to this will have no use. The output can be get from following java code.
 I  used a main method just show how its done. Following is the main method code

 

public static void main(String[] args) {
try {
Object[] cols= {"Subject", "Result"};
DefaultTableModel dtm=new DefaultTableModel(cols, 0);
dtm.addRow(new Object[]{"Mathematics","B"});
dtm.addRow(new Object[]{"Physics","A"});
dtm.addRow(new Object[]{"Chemistry","B"});

JRTableModelDataSource jrtAL = new JRTableModelDataSource(dtm); //Add DefaultTableModel to the //JRTableModel

Map SUB_DATA1=new HashMap(); //This will hold A/L Data
SUB_DATA1.put("jrtAL", jrtAL);
dtm=new DefaultTableModel(cols, 0);
dtm.addRow(new Object[]{"Mathematics","B"});
dtm.addRow(new Object[]{"Physics","A"});
dtm.addRow(new Object[]{"Chemistry","B"});

JRTableModelDataSource jrtOL = new JRTableModelDataSource(dtm);
Map SUB_DATA2=new HashMap();
SUB_DATA2.put("jrtOL", jrtOL);
List data=new ArrayList();
Map dataMap=new HashMap();
dataMap.put("topic", "Web Developing\nCertificate");
dataMap.put("desc", "Diploma in Web Developing\n(Global Solution Technologies) ");
data.add(dataMap);
dataMap=new HashMap();
dataMap.put("topic", "Computer Certificates");
dataMap.put("desc", "Diploma in computer Technology\n(Microtech Computer Systems) ");
data.add(dataMap);
JRMapCollectionDataSource subDS=new JRMapCollectionDataSource(data);

Map SUB_DATA3=new HashMap();
SUB_DATA3.put("subDS", subDS);

String repSource = "./src/report/CVReport.jrxml";
Map param = new HashMap();
param.put("SUB_DATA1", SUB_DATA1);
param.put("SUB_DATA2", SUB_DATA2);
param.put("SUB_DATA3", SUB_DATA3);
///////////////////////////////////////////////////////
Map simpleMasterMap = new HashMap ();
String name="San Buddy CV";
String address="No 1, Somewhere, Somewhere ";
String resTel="091-1234567";
String phone="071- 6204835";
String email="somebody@gmail.com";
String profile="To excel in the field of web developing relating to business and academic industry, "
+ "with highly proven leadership skills involving developing projects, managing projects and ability to"
+ " work with own initiative or and as part of team, is now seeking a profession in the field of web "
+ "application development and willing to dedicate myself strictly to adhere the employment ethics and to"
+ " thrive my best for the respective company�s success. ";

simpleMasterMap.put("name", name);
simpleMasterMap.put("address", address);
simpleMasterMap.put("resTel", resTel);
simpleMasterMap.put("phone", phone);
simpleMasterMap.put("email", email);
simpleMasterMap.put("profile", profile);

List simpleMasterList = new ArrayList();
simpleMasterList.add(simpleMasterMap);
JRMapCollectionDataSource simpleDS = new JRMapCollectionDataSource(simpleMasterList);

JasperReport jr = JasperCompileManager.compileReport(repSource);
JasperPrint jp = JasperFillManager.fillReport(jr, param, simpleDS);
JasperViewer.viewReport(jp);
} catch (JRException ex) {
JOptionPane.showMessageDialog(null, "Jasper Error: \n"+ex.getMessage());
}
}

 

With this, jasper report produced a report like this

Here,

  • Advanced Level
  • Ordinary Level
  • Extra Educational Qualifications

were made with subreports. This is how it looked like in IReport 4.5
CVReport.jrxml 

 

CVSubOL.jrxml 

 

CVSub3.jrxml    

CVExtra.jrxml  

 

In the CVReport, all details have fields that were passed in main method using “simpleDS” JRMapCollectionDataSource. You must create three parameters with SUB_DATA1, SUB_DATA2 and SUB_DATA3 each having parameter class of java.util.HashMap pass dataresources to subreports. You can edit this in Properties tab after selecting parameter created in the report inspector or can write following code in jrxml

<parameter name="SUB_DATA1" class="java.util.HashMap"/>
<parameter name= "SUB_DATA1" class="java.util.HashMap"/>
<parameter name="SUB_DATA1" class="java.util.HashMap "/>

I have used three detail bands to insert subreports. You can add subreports by drag and dropping a sub report to the editing report or by editing XML. Subreport is also a normal jasper report and only difference it makes is, we use it as a sub report to a main report. Here is the code of the subreport code in the main report.

 

<band height=”85″ splitType=”Stretch”>
<subreport>
<reportElement x=”0″ y=”22″ width=”555″ height=”47″/>
<subreportParameter name=”id1″>
<subreportParameterExpression><![CDATA[$P{SUB_DATA1}.get(“jrtAL”)]] ></subreportParameter>
<dataSourceExpression><![CDATA[$P{SUB_DATA1}.get(“jrtAL”)]]>
<subreportExpression>
<![CDATA[$P{SUBREPORT_DIR} + “CVSub3.jasper”]]>
</subreport>
<staticText>
<reportElement x=”19″ y=”2″ width= “163” height=”20″/>
<textElement>
<font fontName=”Calibri” size= “15” isBold=”true”/>
</textElement>
<text><![CDATA[Advanced Level (2011)]]>
</staticText>
</band>

I have used SUB_DATA1 jrtAL dataresource for first CVSub3 subreport. As SUB_DATA1 is a HashMap class parameter, you can use get(String) method to get a object.In CVSub3, I have used two fields “Subject” and “Result” to print the table in detail band. Simply it will do the work of sub reporting.*You must change the language of the main report to groovy and subreport language to java. Otherwise it will bring out a compile error.Likewise you can create other two subreports and little change that I have done in SUB_DATA3 wont affect it. There, I have used a JRMapCollectionDataSource “subDS” and you can get details using two fields “desc” and “topic”.Other two subreport declarations are below.

 

<band height="83">
<subreport>
<reportElement x="0" y="20" width="555" height="48"/>
<subreportParameter name="id2">
<subreportParameterExpression><![CDATA[$P{SUB_DATA2}.get("jrtOL")]]></subreportParameterExpression>
</subreportParameter>
<dataSourceExpression><![CDATA[$P{SUB_DATA2}.get("jrtOL ")]]></dataSourceExpression>
<subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "CVSubOL.jasper "]]></subreportExpression>
</subreport>
<staticText>
<reportElement x="19" y="0" width="163" height="20"/>
<textElement>
<font fontName="Calibri" size="15" isBold="true"/>
</textElement>
<text><![CDATA[Ordinary Level (2008)]]>
</staticText>
</band>

////////////////////////////////////////////////////////////////////////

 

<band height="86">
<subreport>
<reportElement x="0" y="26" width="555" height="60"/>
<subreportParameter name="id2">
<subreportParameterExpression><![CDATA[$P{SUB_DATA3}.get ("subDS")]]></subreportParameterExpression>
</subreportParameter>
<dataSourceExpression><![CDATA[$P{SUB_DATA3}.get("subDS")]]></dataSourceExpression>
<subreportExpression>![CDATA[$P{SUBREPORT_DIR} + "CVExtra.jasper"]]></subreportExpression>
</subreport>
<staticText>
<reportElement x="19" y="0" width= "276" height="26"/>
<textElement>
<font fontName="Calibri" size= "15" isBold="true"/>
</textElement>
<text><![CDATA[Extra Educational Qualifications]]></text>
</staticText>
</band>

 

JAVA RMI Observer Slider
Coder Kai
A humble developer

Categories

  • android 3
  • Apollo Client 1
  • AWS 8
    • AppSync 5
    • EC2 1
    • EKS 1
    • Route53 1
    • S3 1
  • AWS Amplify 1
  • Chakra UI 1
  • Docker 1
  • Embedded 1
  • EmberJS 1
  • FCM 1
  • Godaddy 1
  • GraphQL 3
  • ios 1
  • Jasper 1
  • Java 10
    • Java 11 1
    • Java 14 1
  • JavaEE 2
  • JavaScript 39
    • Express.js 4
    • Javascript Guide 7
    • Node.js 3
    • react-native 4
    • React.js 17
    • Typescript 1
  • Kubernetes 1
  • machine learning 1
  • Maven 2
  • OCaml 3
  • PostgreSQL 1
  • Python 2
  • react-native 4
  • ReactJS 3
  • sass 1
  • Server 6
  • spark 1
  • Terraform 2
  • Ubuntu 4
  • Uncategorized 1
  • webpack 2

Recent Comments

  • binance註冊 on Java 13 Switch Expressions
  • binance register on Chakra UI Animations
  • binance account on SSL Websocket proxy with Nginx for Graphql Subscriptions

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Archives

  • October 2022 3
  • September 2022 7
  • May 2022 1
  • December 2021 1
  • August 2021 1
  • July 2021 6
  • June 2021 3
  • February 2021 1
  • July 2020 1
  • December 2019 5
  • November 2019 6
  • October 2019 3
  • August 2019 1
  • March 2019 1
  • February 2019 1
  • January 2019 2
  • December 2018 1
  • September 2018 2
  • August 2018 1
  • June 2018 1
  • February 2018 1
  • November 2017 2
  • October 2017 5
  • September 2017 1
  • June 2017 1
  • May 2017 10
Sandny Blog space
Theme by Colorlib Powered by WordPress