1
votes

New to JasperReports and attempting to either: (a) manually write a JRXML or (b) manually program a JasperDesign myself (not using a template). My understanding is that both the XML and the Java object represent the same concept: an empty "shell" of a report without any data in it.

I'm trying to figure out what JR components would be suit my needs, and the reference, the samples as well as many online searches haven't turned back anything that is clear-cut. Most JR documentation seems, at least to a newbie, to be written from an "assumed knowledge" standpoint.

What is the difference between, and appropriate usages for, the following "components" (not really sure what else to call them):

  • Text
  • TextField
  • TextElement
  • StaticText

This images example here shows most of these elements being used, seemingly willy-nilly in a not-so-obvious format.

The root of my question is that I wish to display two types of text-based information; one I am referring to as "fields" (not to be confused with JR fields) and the other I am calling "text blocks" consisting of a header and a body. I would like these fields/text blocks rendered as follows:

"Field":

Name:          John Smith
Age:           42
Summary:       This is an example of a field

And a "text block":

Name:
John Smith

Age:
42

Summary:
This is an example of a text block. "Summary:" is the head, and this is the body.

Same information, just presented differently. I believe that one/several of the JR components listed above are the one's I need to use, but I can't seem to find any documentation that confirms/rejects that.

Anybody have any ideas on this? Thanks in advance!

2

2 Answers

1
votes

You're right in that the .jrxml and the JasperDesign object are analogous and represent an empty report design. This design can then be compiled into a .jasper file or JasperReport object, which is then filled with data to produce an actual report.

Out of the four "components" you listed, only TextField and StaticText are really report components. The others simply hold properties of a parent element.

  • StaticText holds text that never changes. It is set in the design and that's it.
  • A TextField has an expression that is executed when the report is filled. These are the elements you will you to put data into your report.
  • Text is a tag that holds the actual text content of a StaticText element. This is the only place it will appear.
  • TextElement declares properties that are specific to how the report should render text. Every TextField or StaticText can include this tag. Properties include text alignment, rotation, typeface, font size, etc.

To solve your problem, you will need to use StaticText components for the name, age and summary labels, as they will always be the same; and TextField components for the actual report data.

As others have suggested, I would recommend iReport. I would do most of the design using it, then make and changes to the JRXML by hand (The xml it produces is a bit bloated). The designer will hide Text and TextElement from you and it will appear as though the properties are being set on the text components themselves. This isn't a problem, but if you're going to change the JRXML by hand you should be aware of it so you don't add attributes to the wrong tags. Looking at the output of iReport is also a good way to learn valid JRXML, as the error messages you get for an incorrect .jrxml file are not always helpful.

Hope that helps!

0
votes

If you don't want to use iReport you can try to build reports dynamically with help of DynamicJasper Java API or JasperReports Java API.

My advice to you: do not try to reinvent the wheel. Try to use iReport designer, it is a really helpful tool for building both easy and complex reports.
Just one cross of using iReport: the JasperServer application (ready for use web application for deploying, building reports and exporting data to different formats) works with jrxml templates.

The iReport Ultimate Guide and JasperReports Ultimate Guide are very valuable books. You can find some manuals here.

This is the snippet of jrxml template relevant to your question (I hope). It was written with iReport.

<detail>
    <band height="60" splitType="Stretch">
        <staticText>
            <reportElement x="0" y="0" width="63" height="20"/>
            <box leftPadding="10">
                <topPen lineWidth="1.0"/>
                <leftPen lineWidth="1.0"/>
            </box>
            <textElement/>
            <text><![CDATA[Name:]]></text>
        </staticText>
        <staticText>
            <reportElement x="0" y="20" width="63" height="20"/>
            <box leftPadding="10">
                <leftPen lineWidth="1.0"/>
            </box>
            <textElement/>
            <text><![CDATA[Age:]]></text>
        </staticText>
        <staticText>
            <reportElement x="0" y="40" width="63" height="20"/>
            <box leftPadding="10">
                <leftPen lineWidth="1.0"/>
                <bottomPen lineWidth="1.0"/>
            </box>
            <textElement/>
            <text><![CDATA[Summary:]]></text>
        </staticText>
        <textField>
            <reportElement x="63" y="0" width="290" height="20"/>
            <box leftPadding="10">
                <topPen lineWidth="1.0"/>
                <rightPen lineWidth="1.0"/>
            </box>
            <textElement/>
            <textFieldExpression><![CDATA[$F{Name}]]></textFieldExpression>
        </textField>
        <textField>
            <reportElement x="63" y="20" width="290" height="20"/>
            <box leftPadding="10">
                <pen lineWidth="1.0"/>
                <topPen lineWidth="0.0"/>
                <leftPen lineWidth="0.0"/>
                <bottomPen lineWidth="0.0"/>
                <rightPen lineWidth="1.0"/>
            </box>
            <textElement/>
            <textFieldExpression><![CDATA[$F{Age}]]></textFieldExpression>
        </textField>
        <textField>
            <reportElement x="63" y="40" width="290" height="20"/>
            <box leftPadding="10">
                <bottomPen lineWidth="1.0"/>
                <rightPen lineWidth="1.0"/>
            </box>
            <textElement/>
            <textFieldExpression><![CDATA[$F{Summary}]]></textFieldExpression>
        </textField>
    </band>
</detail>

The result will be: enter image description here

It takes me just 5 minutes.