Table of contents in Jasper Report

I need to include at the bottom of a report a Table of Contents,
for example group header and page where it is.
I saw the example than is included in Jasper Report that use Scriptlets but I don’t undertand.

If someone have a example or where could I find a example on Internet , great thanks.

Armin …

Ohhh Yeahh!

2 Weeks of Headdaches for that Table of Content Thing.
First of all, you need a Java Class to Store your Data for the Table of Content:

package bewerberhandoutapp;

import java.io.Console;
import java.lang.System;

public class HeadingBean
{

	private String headingText = null;
	private Integer pageIndex = null;

	public HeadingBean(

		String text,
		Integer pageIndex
		)
	{
		this.headingText = text;
		this.pageIndex = pageIndex;               
	}


	public String getHeadingText()
	{
		return this.headingText;
	}

	public Integer getPageIndex()
	{
		return this.pageIndex;
	}


}

Then you need a class where you are creating Objects of your Data.

public class HeadingScriptlet extends JRDefaultScriptlet

{
        public Boolean addHeading() throws JRScriptletException
	{
                Integer kontaktid = (Integer)this.getFieldValue("KontaktId");
                Collection headings = (Collection)this.getVariableValue("HeadingsCollection");
                String vorname = null;
                String nachname = null;
                String anrede = null;
                String titel = null;
                String name = null;
                String insertname = null;
                Integer pageindex = (Integer)this.getVariableValue("PAGE_NUMBER");
                vorname = (String)this.getFieldValue("Vorname").toString().trim();
                nachname = (String)this.getFieldValue("Nachname").toString().trim();
                anrede = (String)this.getFieldValue("Anrede").toString().trim();
                titel = (String)this.getFieldValue("Titel").toString().trim();
                name = anrede.trim() + " " + titel.trim() + (titel.equalsIgnoreCase("") || titel.equalsIgnoreCase(null) ? "" : " ") + nachname.trim() + ", " + vorname;
                insertname = (String)name;
                try
                {
                headings.add(new <packageName>.HeadingBean(insertname, pageindex));
                }
                catch (Exception e)
                {
                    System.out.println(e);
                }

                public String Caller() throws JRScriptletException
                {
                addHeading();
                String ausgabe = null;
                return ausgabe;
               }
              
      }
}

= the name of your *.jar

Compile these 2 classes.

in iReport:

Include the jar file you created to the classpath of iReport.
Add in the Properties of your Report the scriptlet class:
.HeadingScriptlet
Now you need to declare a variable.
Call it:
HeadingsCollection
its of type:
java.utils.collection
initial value:
new java.util.ArrayList()
calculation:
system

Create a subreport in the summary band.
Connection Type of the subreport:
Use a datasource expression

Datasource expression is:
new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($V{HeadingsCollection})

In the subreport, you click on the SQL Button.
There you have to go to the JavaBean Datasource Tab.
In the first name you put your package and class Name.
eg.
.HeadingScriptlet

Then iReport should show you the Properties of your class.
In our case:
pageIndex
headingText

These Two fields you can use just like a normal field.

Back to the Main Report:
Add a field of type String to your report, where you actually want to push your data to the Bean.
In that field you have to write:
$P{REPORT_SCRIPTLET}.Caller()

Run your Report.

I hope i didnt forget any steps :)

Have fun while sorting it out. I had lots of it! :twisted:

Greets
Jan

Hi
Thanks for sharing the information.
I am new to jasper and not much expert in java.I tried to make an example using your steps.
I copied your example classes included then in a project import libraries for the source code to compile and then using .class files I made a jar file.
I used the jar file in the path of ireports but while selecting datasource I gave to name of package and class file but system could not load the attribes saying unable to load class check the path.

Can I please request to review the files I sent and if possible can you please elaborate little more by including .jrxml and jar file that you used.
As I am mainly stuck at the step where I have to load class attributes so if possible add some light on the matter.

Thanks
Asim[attachment=0]tocjasper.rar[/attachment]

tocjasper.rar (1015 Bytes)