How to organize Frames in Jasper Report so they float flawlessly? - jasper-reports

In a Jasper report I have 4 Frames (below image) which have some properties set as shown in the same image.
Now, the idea is that given certain parameters showBlue and showRed the frames BLUE and RED respectively are shown or hidden and the subsequent frame "floats" after the previous one (taking in consideration the following order: BLUE <- RED <- GREEN) while BLACK should stay in the same place.
The two left frames (RED and GREEN) were floating up perfectly before the BLACK one was set in place. After that when I set the parameters showBlue and showRed to false (hide BLUE and RED frames) this is the result:
This is the jrxml for the report design:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.5.1.final using JasperReports Library version 6.5.1 -->
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="Float_UP" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="6578bc34-0c2e-4179-99da-5ec1dd90a422">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
<parameter name="showBlue" class="java.lang.Boolean"/>
<parameter name="showRed" class="java.lang.Boolean"/>
<queryString>
<![CDATA[]]>
</queryString>
<detail>
<band height="211" splitType="Stretch">
<staticText>
<reportElement x="0" y="0" width="400" height="12" uuid="54cb1704-59d8-4272-9ec1-db4cea913cd3"/>
<text><![CDATA[Header 1 (right before conditional frame)]]></text>
</staticText>
<frame>
<reportElement x="0" y="15" width="400" height="66" isRemoveLineWhenBlank="true" uuid="0140ba9b-f2f0-494c-82bc-caf6b5efc63e">
<printWhenExpression><![CDATA[$P{showBlue}]]></printWhenExpression>
</reportElement>
<box>
<pen lineWidth="3.0" lineColor="#2E0DD4"/>
</box>
<staticText>
<reportElement x="1" y="1" width="379" height="59" uuid="a8b7d505-a6ad-4359-9263-23ac087b19ff"/>
<textElement>
<font size="14"/>
</textElement>
<text><![CDATA[BLUE: `isRemoveLineWhenBlank=true` and `printWhenExpression` set]]></text>
</staticText>
</frame>
<frame>
<reportElement positionType="Float" x="0" y="85" width="400" height="60" isRemoveLineWhenBlank="true" uuid="c80dd879-ce81-4921-b17e-9882763a3f61">
<printWhenExpression><![CDATA[$P{showRed}]]></printWhenExpression>
</reportElement>
<box>
<pen lineWidth="3.0" lineColor="#F50A25"/>
</box>
<staticText>
<reportElement x="0" y="0" width="380" height="50" uuid="d0cadbd4-b436-47f4-a32d-2b00f0c6b147"/>
<textElement>
<font size="14"/>
</textElement>
<text><![CDATA[RED: `isRemoveLineWhenBlank=true`, `printWhenExpression` set and `positionType="Float"`]]></text>
</staticText>
</frame>
<frame>
<reportElement positionType="Float" x="2" y="151" width="398" height="60" uuid="e0a5ed13-d8f2-4acd-ac14-1f5633099542"/>
<box>
<pen lineWidth="3.0" lineColor="#22B002"/>
</box>
<staticText>
<reportElement x="1" y="1" width="377" height="39" uuid="482471b4-4c3b-42c6-892d-f8c42ca320bf"/>
<textElement>
<font size="14"/>
</textElement>
<text><![CDATA[GREEN: `positionType="Float"`]]></text>
</staticText>
</frame>
<frame>
<reportElement x="420" y="15" width="130" height="70" uuid="8e057d80-72be-4f66-ae9b-ef80610daf36"/>
<box>
<pen lineWidth="3.0"/>
</box>
<staticText>
<reportElement x="6" y="6" width="118" height="44" uuid="36d8e560-3af5-4edf-b9ef-9b9311877c3a"/>
<textElement>
<font size="14"/>
</textElement>
<text><![CDATA[BLACK]]></text>
</staticText>
</frame>
</band>
</detail>
</jasperReport>
And I have set up a Java Project with Swagger in case you want to run it with maven.
Q: How can I make frames RED and GREEN to properly "float-up" after its previous frame while frame BLACK stays in the same place?

Yes the green will not float up since the black box is placed immediately above it
From JRElement API
Float - The element floats in its parent section if it is pushed downward by other elements found above it. It tries to conserve the distance between it and the neighboring elements placed immediately above it
Solution
The solutions obviously is to not have any element immediately above it and one way to achieve this is to use another frame or a subreport. If you need elements to float up also under this design you probably will need a subreport but I will show the parent frame solution, because it is the easiest way to solve your direct problem.
Example using a parent frame
jrxml
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="Float_UP" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="6578bc34-0c2e-4179-99da-5ec1dd90a422">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
<parameter name="showBlue" class="java.lang.Boolean"/>
<parameter name="showRed" class="java.lang.Boolean"/>
<queryString>
<![CDATA[]]>
</queryString>
<detail>
<band height="96" splitType="Stretch">
<staticText>
<reportElement x="0" y="0" width="400" height="12" uuid="54cb1704-59d8-4272-9ec1-db4cea913cd3"/>
<text><![CDATA[Header 1 (right before conditional frame)]]></text>
</staticText>
<frame>
<reportElement x="0" y="15" width="409" height="78" uuid="82076f1d-b7a9-4b63-ab4a-af54b0817a62"/>
<frame>
<reportElement x="0" y="4" width="400" height="66" isRemoveLineWhenBlank="true" uuid="0140ba9b-f2f0-494c-82bc-caf6b5efc63e">
<printWhenExpression><![CDATA[$P{showBlue}]]></printWhenExpression>
</reportElement>
<box>
<pen lineWidth="3.0" lineColor="#2E0DD4"/>
</box>
<staticText>
<reportElement x="1" y="1" width="379" height="59" uuid="a8b7d505-a6ad-4359-9263-23ac087b19ff"/>
<textElement>
<font size="14"/>
</textElement>
<text><![CDATA[BLUE: `isRemoveLineWhenBlank=true` and `printWhenExpression` set]]></text>
</staticText>
</frame>
<frame>
<reportElement positionType="Float" x="-1" y="75" width="400" height="60" isRemoveLineWhenBlank="true" uuid="c80dd879-ce81-4921-b17e-9882763a3f61">
<printWhenExpression><![CDATA[$P{showRed}]]></printWhenExpression>
</reportElement>
<box>
<pen lineWidth="3.0" lineColor="#F50A25"/>
</box>
<staticText>
<reportElement x="0" y="0" width="380" height="50" uuid="d0cadbd4-b436-47f4-a32d-2b00f0c6b147"/>
<textElement>
<font size="14"/>
</textElement>
<text><![CDATA[RED: `isRemoveLineWhenBlank=true`, `printWhenExpression` set and `positionType="Float"`]]></text>
</staticText>
</frame>
<frame>
<reportElement positionType="Float" x="0" y="140" width="398" height="60" uuid="e0a5ed13-d8f2-4acd-ac14-1f5633099542"/>
<box>
<pen lineWidth="3.0" lineColor="#22B002"/>
</box>
<staticText>
<reportElement x="1" y="1" width="377" height="39" uuid="482471b4-4c3b-42c6-892d-f8c42ca320bf"/>
<textElement>
<font size="14"/>
</textElement>
<text><![CDATA[GREEN: `positionType="Float"`]]></text>
</staticText>
</frame>
</frame>
<frame>
<reportElement x="420" y="15" width="130" height="70" uuid="8e057d80-72be-4f66-ae9b-ef80610daf36"/>
<box>
<pen lineWidth="3.0"/>
</box>
<staticText>
<reportElement x="6" y="6" width="118" height="44" uuid="36d8e560-3af5-4edf-b9ef-9b9311877c3a"/>
<textElement>
<font size="14"/>
</textElement>
<text><![CDATA[BLACK]]></text>
</staticText>
</frame>
</band>
</detail>
</jasperReport>
Output
Additional design notes
As you can see in output there is a 4px height difference between the green and black box, this is due to the empty space you have between the frames. If you need pixel perfect reports you need to have zero space between the components that you are removing/floating.

Maybe you can try using multiple Detail bands for your report. Thus you can use the property printWhenExpresion on each band, instead of in each frame.
Here you can find your jrxml code modified with an example.

Related

The net.sf.jasperreports.style.backcolor attribute is not working for text fields at Frame

In my database I have the hex colour codes for various scenarios and I want to highlight the rows using these variable colours. I have used net.sf.jasperreports.style.backcolor, but all rows have the same background colour which is that of the first row.
<frame>
<reportElement stretchType="ContainerHeight" mode="Opaque" x="0" y="0" width="802" height="12" isRemoveLineWhenBlank="true">
<propertyExpression name="net.sf.jasperreports.style.backcolor"><![CDATA[$F{sp_colour}]]></propertyExpression>
</reportElement>
Looks like the engine is initializing the frame's attribute using the first record from the datasource and using this initial value for all other records (2nd iteration from the result set, 3rd iteration and so on). Probably this is a bug of JasperReports library.
The issue can be solved using several workarounds.
Workaround 1. Using conditional style
The introduction of conditional style is resolving the issue. If we have N unique values of colors we have to add at least N - 1 conditions for style - one condition for one color.
Working example. Datasource
I used csv based datasource for testing report:
team1,team2,color1,color2,duration_color,date
BEL,POR,#0000FF,#7F8C8D,#FFFFFF,"27 June"
ITA,AUT,#0000FF,#7F8C8D,#F2F3F4,"26 June"
FRA,SUI,#7F8C8D,#0000FF,#F6DDCC,"28 June"
CRO,ESP,#7F8C8D,#0000FF,#F2F3F4,"28 June"
SWE,UKR,#7F8C8D,#0000FF,#F2F3F4,"29 June"
ENG,GER,#0000FF,#7F8C8D,#FFFFFF,"29 June"
NED,CZE,#7F8C8D,#0000FF,#FFFFFF,"27 June"
WAL,DEN,#7F8C8D,#0000FF,#FFFFFF,"26 June"
The first row at the file contains the column names and should be skipped (for example using settings of data adapter).
This file contains 3 unique colors (the field duration_color) that will be used as background color.
Working example. Report template
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="Backcolor, W1" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="595" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="colors"/>
<style name="frameStyle">
<conditionalStyle>
<conditionExpression><![CDATA["#FFFFFF".equals($F{duration_color})]]></conditionExpression>
<style/>
</conditionalStyle>
<conditionalStyle>
<conditionExpression><![CDATA["#F2F3F4".equals($F{duration_color})]]></conditionExpression>
<style/>
</conditionalStyle>
<conditionalStyle>
<conditionExpression><![CDATA["#F6DDCC".equals($F{duration_color})]]></conditionExpression>
<style/>
</conditionalStyle>
</style>
<field name="team1" class="java.lang.String"/>
<field name="team2" class="java.lang.String"/>
<field name="color1" class="java.lang.String"/>
<field name="color2" class="java.lang.String"/>
<field name="duration_color" class="java.lang.String"/>
<field name="date" class="java.lang.String"/>
<detail>
<band height="20">
<frame>
<reportElement style="frameStyle" mode="Opaque" x="172" y="0" width="250" height="20">
<propertyExpression name="net.sf.jasperreports.style.backcolor"><![CDATA[$F{duration_color}]]></propertyExpression>
</reportElement>
<textField>
<reportElement x="0" y="0" width="100" height="20"/>
<textElement textAlignment="Left" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[String.format("Match date: %s", $F{date})]]></textFieldExpression>
</textField>
<textField>
<reportElement x="100" y="0" width="60" height="20">
<propertyExpression name="net.sf.jasperreports.style.forecolor"><![CDATA[$F{color1}]]></propertyExpression>
</reportElement>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{team1}]]></textFieldExpression>
</textField>
<staticText>
<reportElement x="160" y="0" width="30" height="20"/>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<text><![CDATA[vs]]></text>
</staticText>
<textField>
<reportElement x="190" y="0" width="60" height="20">
<propertyExpression name="net.sf.jasperreports.style.forecolor"><![CDATA[$F{color2}]]></propertyExpression>
</reportElement>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{team2}]]></textFieldExpression>
</textField>
</frame>
</band>
</detail>
<summary>
<band height="50">
<staticText>
<reportElement mode="Opaque" x="20" y="0" width="100" height="20" backcolor="#F2F3F4"/>
<text><![CDATA[Extra time]]></text>
</staticText>
<staticText>
<reportElement mode="Opaque" x="20" y="20" width="100" height="20" backcolor="#F6DDCC"/>
<text><![CDATA[Penalty shootout]]></text>
</staticText>
</band>
</summary>
</jasperReport>
The 3 conditional styles were introduced at frameStyle and this style is applied for the whole frame.
Working example. The output result
I used Jaspersoft Studio (JSS) to generate the output result.
Workaround 2. Applying attributes at elements, not at frame
Another workaround is to apply the net.sf.jasperreports.style.backcolor attribute at all elements (textField and staticText) instead of using the frame's attribute.
Working example. Report template
No need to introduce conditional style in this case.
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="Backcolor, W2" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="595" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="colors"/>
<field name="team1" class="java.lang.String"/>
<field name="team2" class="java.lang.String"/>
<field name="color1" class="java.lang.String"/>
<field name="color2" class="java.lang.String"/>
<field name="duration_color" class="java.lang.String"/>
<field name="date" class="java.lang.String"/>
<detail>
<band height="20">
<frame>
<reportElement x="172" y="0" width="250" height="20"/>
<textField>
<reportElement mode="Opaque" x="0" y="0" width="100" height="20"/>
<textElement textAlignment="Left" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[String.format("Match date: %s", $F{date})]]></textFieldExpression>
</textField>
<textField>
<reportElement mode="Opaque" x="100" y="0" width="60" height="20">
<propertyExpression name="net.sf.jasperreports.style.forecolor"><![CDATA[$F{color1}]]></propertyExpression>
<propertyExpression name="net.sf.jasperreports.style.backcolor"><![CDATA[$F{duration_color}]]></propertyExpression>
</reportElement>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{team1}]]></textFieldExpression>
</textField>
<staticText>
<reportElement mode="Opaque" x="160" y="0" width="30" height="20">
<propertyExpression name="net.sf.jasperreports.style.backcolor"><![CDATA[$F{duration_color}]]></propertyExpression>
</reportElement>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<text><![CDATA[vs]]></text>
</staticText>
<textField>
<reportElement mode="Opaque" x="190" y="0" width="60" height="20">
<propertyExpression name="net.sf.jasperreports.style.forecolor"><![CDATA[$F{color2}]]></propertyExpression>
<propertyExpression name="net.sf.jasperreports.style.backcolor"><![CDATA[$F{duration_color}]]></propertyExpression>
</reportElement>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{team2}]]></textFieldExpression>
</textField>
</frame>
</band>
</detail>
<summary>
<band height="50">
<staticText>
<reportElement mode="Opaque" x="20" y="0" width="100" height="20" backcolor="#F2F3F4"/>
<text><![CDATA[Extra time]]></text>
</staticText>
<staticText>
<reportElement mode="Opaque" x="20" y="20" width="100" height="20" backcolor="#F6DDCC"/>
<text><![CDATA[Penalty shootout]]></text>
</staticText>
</band>
</summary>
</jasperReport>
The frame even can be removed.
Working example. The output result
The result generated at JSS is absolutely the same:
Thanks you Alex K.
The first option of using conditional formatting was not going to work as the users can pick a large number of colours
The second option of applying the net.sf.jasperreports.style.backcolor attribute to all elements was where I was thinking of heading, but with 2 details lines with 12+ elements in each was going to be time consuming.
Instead I added a textbox element the same size as the frame, as the first element in the frame and applied the net.sf.jasperreports.style.backcolor attribute to it. I used a printWhenExpression as the colouring was only required for special make and therefore retained alternate row highlighting for the other rows in the report
<frame>
<reportElement style="Row" stretchType="ContainerHeight" mode="Opaque" x="0" y="0" width="802" height="12" isRemoveLineWhenBlank="true" uuid="b9bde183-f2ec-4051-8c7e-22072071357d"/>
<textField isStretchWithOverflow="true" isBlankWhenNull="true">
<reportElement mode="Opaque" x="0" y="0" width="798" height="12" uuid="d110cab3-6534-4c2a-9304-74a11c2a7cb8">
<propertyExpression name="net.sf.jasperreports.style.backcolor"><![CDATA[$F{sp_colour}]]></propertyExpression>
<printWhenExpression><![CDATA["Y".equals($F{is_special_make})]]>/printWhenExpression>
</reportElement>
<textElement verticalAlignment="Middle">
<font fontName="Lucida Sans" size="6"/>
</textElement>
</textField>

How can I freeze the first row of the second sheet in an excel-export?

I'm trying to freeze the first row on the second sheet of my excle export from a jasper report, but it freezes another row. In my example code below it freezes the first two rows on the second sheet, but if there are more rows on the first sheet the number of rows frozen on the second sheet are even higher. It seems like it "adds" the number of rows from the first sheet on top of the second sheet, so if there are e.g. 9 rows on the first sheet and I try to freeze the first row on the second sheet, it freezes the 10th row on my second sheet.
Is this a bug in jasper-reports or am I doing something wrong here? Does Anybody know how to solve this problem in another way?
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="RowBreakTest" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="0292da25-2332-41a0-8f81-fcbe04339e99">
<title>
<band height="28">
<staticText>
<reportElement x="0" y="0" width="280" height="14" />
<text><![CDATA[This is just the first sheet]]></text>
</staticText>
<staticText>
<reportElement x="0" y="14" width="280" height="14" />
<text><![CDATA[]]></text>
</staticText>
<staticText>
<reportElement x="280" y="0" width="270" height="14" />
<text><![CDATA[]]></text>
</staticText>
<staticText>
<reportElement x="280" y="14" width="270" height="14" />
<text><![CDATA[]]></text>
</staticText>
</band>
</title>
<columnHeader>
<band height="14" splitType="Stretch">
<staticText>
<reportElement x="0" y="0" width="280" height="14">
<property name="net.sf.jasperreports.export.xls.break.before.row" value="true"/>
<property name="net.sf.jasperreports.export.xls.freeze.column.edge" value="Left"/>
</reportElement>
<text><![CDATA[header on 2nd sheet]]></text>
</staticText>
<staticText>
<reportElement x="280" y="0" width="270" height="14">
<property name="net.sf.jasperreports.export.xls.freeze.row.edge" value="Bottom"/>
</reportElement>
<text><![CDATA[more header on 2nd sheet]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="28" splitType="Stretch">
<staticText>
<reportElement x="0" y="0" width="280" height="14" />
<text><![CDATA[data]]></text>
</staticText>
<staticText>
<reportElement x="0" y="14" width="280" height="14" />
<text><![CDATA[more data]]></text>
</staticText>
<staticText>
<reportElement x="280" y="0" width="270" height="14" />
<text><![CDATA[even more data]]></text>
</staticText>
<staticText>
<reportElement x="280" y="14" width="270" height="14"/>
<text><![CDATA[a lot more data]]></text>
</staticText>
</band>
</detail>
</jasperReport>

How to insert empty row at the table based on condition in case using JRBeanCollectionDataSource?

There is a list and I use this mechanism to pass it to my jasper report:
JRBeanCollectionDataSource jrBeanCollectionDataSource = new JRBeanCollectionDataSource(list);
The Object of that list is as follows:
public class Test{
private int id;
//other properties + getters & setter
}
Now Assume I have 10 elements within my list (id= 1 to 10) and I want to separate them in one table using an empty row, meaning 5 rows top, one empty row, 5 rows down.
How to do that ?
The subreport element can helps with solving this problem - we can show or hide subreport based on condition using printWhenExpression.
In your case we need to show subreport with one empty line.
Example
The subreport for showing empty line is very simple. It does not need datasource for showing data - we can put staticText right at Title band. We don't need also margins.
The jrxml will be:
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="Empty Line" pageWidth="572" pageHeight="30" whenNoDataType="AllSectionsNoDetail" columnWidth="572" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0">
<style name="bordered">
<box>
<pen lineWidth="0.25"/>
</box>
</style>
<title>
<band height="30" splitType="Stretch">
<staticText>
<reportElement style="bordered" x="0" y="0" width="572" height="30"/>
</staticText>
</band>
</title>
</jasperReport>
I used simple csv datasource for example - file films.csv contains data for report:
id,name,year,rating
1,The Shawshank Redemption,1994,9.3
2,The Godfather,1972,9.2
3,The Dark Knight,2008,9.0
4,The Godfather: Part II,1974,9.0
5,The Lord of the Rings: The Return of the King,2003,8.9
6,Pulp Fiction,1994,8.9
7,Schindler's List,1993,8.9
8,"The Good, the Bad and the Ugly",1966,8.9
9,12 Angry Men,1957,8.9
10,Avengers: Endgame,2019,8.8
The main (master) report is using CSV File Data Adapter for building report.
The jrxml of master report:
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="Insert blank row on condition" pageWidth="612" pageHeight="792" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" >
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="films.csv"/>
<style name="bordered">
<box>
<pen lineWidth="0.25"/>
</box>
</style>
<field name="id" class="java.lang.Integer"/>
<field name="name" class="java.lang.String"/>
<field name="year" class="java.lang.String"/>
<field name="rating" class="java.math.BigDecimal"/>
<title>
<band height="30" splitType="Stretch">
<staticText>
<reportElement x="158" y="0" width="256" height="30"/>
<text><![CDATA[Films. Showing empty line for Id == 5]]></text>
</staticText>
</band>
</title>
<columnHeader>
<band height="30">
<staticText>
<reportElement x="0" y="0" width="70" height="30"/>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<text><![CDATA[Id]]></text>
</staticText>
<staticText>
<reportElement x="70" y="0" width="290" height="30"/>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<text><![CDATA[Name]]></text>
</staticText>
<staticText>
<reportElement x="360" y="0" width="69" height="30"/>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<text><![CDATA[Year]]></text>
</staticText>
<staticText>
<reportElement x="429" y="0" width="143" height="30"/>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<text><![CDATA[Rating]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="60">
<subreport>
<reportElement x="0" y="30" width="572" height="30" isRemoveLineWhenBlank="true" >
<printWhenExpression><![CDATA[$F{id} == 5]]></printWhenExpression>
</reportElement>
<subreportExpression><![CDATA["empty_row.jasper"]]></subreportExpression>
</subreport>
<textField>
<reportElement style="bordered" x="0" y="0" width="70" height="30"/>
<textElement textAlignment="Center"/>
<textFieldExpression><![CDATA[$F{id}]]></textFieldExpression>
</textField>
<textField>
<reportElement style="bordered" x="70" y="0" width="290" height="30"/>
<textElement textAlignment="Center"/>
<textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
</textField>
<textField>
<reportElement style="bordered" x="360" y="0" width="69" height="30"/>
<textElement textAlignment="Center"/>
<textFieldExpression><![CDATA[$F{year}]]></textFieldExpression>
</textField>
<textField>
<reportElement style="bordered" x="429" y="0" width="143" height="30"/>
<textElement textAlignment="Center"/>
<textFieldExpression><![CDATA[$F{rating}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
I used <printWhenExpression><![CDATA[$F{id} == 5]]></printWhenExpression> for hiding subreport with empty line, it means that empty line will show only of row with id == 5. You can use any expression you want.
The output at JSS will be:
Alternative solutions
You can insert element during building collection before passing this collection at JRBeanCollectionDataSource.
You can implement custom DataSource based on JRBeanCollectionDataSource with some logic.

jasper report is not showing table when using java bean datasource

I am using JasperReports in my project using table component in the summary band and
providing data with JRBeanCollectionDataSource but after extracting the report getting only title of the report and table is not displaying.
Here is my .jrxml
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="CampaignSummary" language="groovy" pageWidth="1400" pageHeight="1000" orientation="Landscape" columnWidth="1360" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="ef078799-c539-490f-b244-d493f4522ef2">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="163"/>
<property name="ireport.y" value="0"/>
<style name="table">
<box>
<pen lineWidth="1.0" lineColor="#000000"/>
</box>
</style>
<style name="table_TH" mode="Opaque" backcolor="#00FFFF"/>
<style name="table_CH" mode="Opaque" backcolor="#BFFFFF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
</box>
</style>
<style name="table_TD" mode="Opaque" backcolor="#FFFFFF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
</box>
<conditionalStyle>
<conditionExpression><![CDATA[new Boolean($V{REPORT_COUNT}.intValue()%2==0)]]></conditionExpression>
<style backcolor="#EFFFFF"/>
</conditionalStyle>
</style>
<subDataset name="TableDataset" uuid="c99fd89d-9d8f-488f-95db-360a7f5aba96">
<field name="CampaignID" class="java.lang.String">
<fieldDescription><![CDATA[me.CampaignID]]></fieldDescription>
</field>
<field name="CampaignName" class="java.lang.String">
<fieldDescription><![CDATA[Campaign Name]]></fieldDescription>
</field>
.
.
.
......some more fields....
.
.
<field name="NetImpact" class="java.lang.String">
<fieldDescription><![CDATA[Net Impact]]></fieldDescription>
</field>
</subDataset>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="54" splitType="Stretch">
<staticText>
<reportElement uuid="8b41287b-3930-416a-ba00-7f4a9c907e26" x="0" y="0" width="1360" height="54"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="Arial Rounded MT Bold" size="24"/>
</textElement>
<text><![CDATA[Campaign Summary Report]]></text>
</staticText>
</band>
</title>
<summary>
<band height="239" splitType="Stretch">
<frame>
<reportElement uuid="61313d06-6df1-4212-ab8f-8e311dc5ad33" x="0" y="0" width="1360" height="87"/>
</frame>
<frame>
<reportElement uuid="c04b08f6-89d3-4104-87ba-adecd3c4f7dd" x="0" y="87" width="1360" height="152"/>
<componentElement>
<reportElement uuid="8afc21c1-7b57-4dab-8178-8810c1183dd9" key="table" style="table" x="0" y="0" width="1360" height="152"/>
<jr:table xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd">
<datasetRun subDataset="TableDataset">
<dataSourceExpression>
<![CDATA[($P{REPORT_DATA_SOURCE})]]>
</dataSourceExpression>
</datasetRun>
<jr:column width="90" uuid="eece2ecf-027d-4ef1-bd05-8d7d6bbbc0b0">
<jr:columnHeader style="table_CH" height="30" rowSpan="1">
<staticText>
<reportElement uuid="c0d108a0-9f4b-4ec3-b9d0-664ea0724584" x="0" y="0" width="90" height="30"/>
<textElement/>
<text><![CDATA[Campaign Name]]></text>
</staticText>
</jr:columnHeader>
<jr:detailCell style="table_TD" height="20" rowSpan="1">
<textField>
<reportElement uuid="6a8f7bf6-5e9b-4f77-a524-9a8d7f0ee2ca" x="0" y="0" width="90" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[String.valueOf($F{CampaignName})]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
.
.
.some more columns...
.
<jr:column width="80" uuid="312f9143-cee8-46fb-a296-27172c4fca98">
<jr:columnHeader style="table_CH" height="30" rowSpan="1">
<staticText>
<reportElement uuid="22ec2935-d4a9-4b64-856d-e64d72f0d856" x="0" y="0" width="80" height="30"/>
<textElement/>
<text><![CDATA[Net Impact]]></text>
</staticText>
</jr:columnHeader>
<jr:detailCell style="table_TD" height="20" rowSpan="1">
<textField>
<reportElement uuid="071cddad-63f1-47bc-8da5-a2d6752e3abe" x="0" y="0" width="80" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[String.valueOf($F{NetImpact})]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
</jr:table>
</componentElement>
</frame>
</band>
</summary>
and java code is
CampaignSummaryBeanFactory campaignSummaryBeanFactory =new CampaignSummaryBeanFactory();
JasperFillManager.fillReportToFile("build/reports/CampaignSummary.jasper", null, new JRBeanCollectionDataSource(campaignSummaryBeanFactory.getCampaignSummaryBeans()));
Table will not be displayed if it is empty.
Do you have any element in campaignSummaryBeanFactory.getCampaignSummaryBeans()?
For the case when you have no data, put something in the "No Data" band, some static text like 'No data to display' or even the header of your table, if you want to see the empty table when you have no rows.
As far as you code goes, the table will also be empty if you have 1 element in the datasource. Otherwise it will skeep the first record in the list. See Jasper report missing first row
You will find some people who tells you to put table in summary band. But that is the reason your table is not displaying. Put it in a detail band.

Can JasperReport insert some specified images/content to a particular page number?

Let say i want to print a image on page 2.
If page 1 has a lot of content, page 1 content will split to page 1 and page 3,
while page 2 is still that image.
Can that be done in pdf generation ?
Thanks in advance.
Yes, you can use printWhenExpression property of image element.
The working sample - I add condition to show image on 2nd page only. My report design in iReport is:
The jrxml file is:
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="image_expression" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
<queryString>
<![CDATA[select id from address]]>
</queryString>
<field name="ID" class="java.lang.Integer"/>
<title>
<band height="47" splitType="Stretch">
<image scaleImage="RetainShape">
<reportElement x="247" y="0" width="32" height="33"/>
<imageExpression><![CDATA["Number1.png"]]></imageExpression>
</image>
<staticText>
<reportElement x="147" y="13" width="100" height="20"/>
<textElement textAlignment="Center">
<font isBold="true" isItalic="true"/>
</textElement>
<text><![CDATA[Title]]></text>
</staticText>
</band>
</title>
<detail>
<band height="35" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="59" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$F{ID}]]></textFieldExpression>
</textField>
<image scaleImage="RetainShape">
<reportElement x="59" y="0" width="32" height="33">
<printWhenExpression><![CDATA[$V{PAGE_NUMBER}==2]]></printWhenExpression>
</reportElement>
<imageExpression><![CDATA["Number2.png"]]></imageExpression>
</image>
</band>
</detail>
<pageFooter>
<band height="34">
<image scaleImage="RetainShape">
<reportElement x="523" y="1" width="32" height="33">
<printWhenExpression><![CDATA[$V{PAGE_NUMBER}==2]]></printWhenExpression>
</reportElement>
<imageExpression><![CDATA["Number3.png"]]></imageExpression>
</image>
<textField>
<reportElement x="44" y="14" width="80" height="20"/>
<textElement textAlignment="Right"/>
<textFieldExpression><![CDATA["Page "+$V{PAGE_NUMBER}+" of"]]></textFieldExpression>
</textField>
<textField evaluationTime="Report">
<reportElement x="124" y="14" width="40" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[" " + $V{PAGE_NUMBER}]]></textFieldExpression>
</textField>
</band>
</pageFooter>
</jasperReport>
The result will be (via preview function in iReport), first page of report will be:
and the second page:
and the last third page:
UPDATE:
You can add variable for counting row's number at the page and use this variable in printWhenExpression.
For example, if I want to show image only on the second page at the third row my template will be like this:
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport ...>
...
<variable name="rowAtPage" class="java.lang.Integer" resetType="Page">
<variableExpression><![CDATA[$V{rowAtPage} + 1]]></variableExpression>
<initialValueExpression><![CDATA[0]]></initialValueExpression>
</variable>
...
<detail>
<band height="35" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="59" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$F{ID}]]></textFieldExpression>
</textField>
<image scaleImage="RetainShape">
<reportElement x="59" y="0" width="32" height="33">
<printWhenExpression><![CDATA[$V{PAGE_NUMBER} == 2 && $V{rowAtPage} == 3]]></printWhenExpression>
</reportElement>
<imageExpression><![CDATA["Number2.png"]]></imageExpression>
</image>
</band>
</detail>
...
</jasperReport>
The result will be: