My boss asked me today did I use JFlex and can I provide quick tutorial.
I used JFLex, JCUp... made some compiling efforts back then. But I can't remember a thing.
So, my first effort in getting serious :) will be my work in progress, using JasperReports and IReport.
IReport crash course
Ok, what is important.
Go to View->Fields. Field names are in my example "myObjectAttribute1" and "myObjectAttribute2". I'll use them later in Java for my data source method "getFieldValue(JRField jrField)". And thats all for simple report! :)
Put some static text, some pictures... and your text fields for "myObjectAttributes". Something like this:
Now save as "MyTemplateName.jrxml".
Compile.
Put it where your Java code can find it.
That's all.
What you need in your Java code
First you need a DataSource for your report. I needed swing table in my report so I'm getting my table model ArrayList and making MyDataSource:
public class CustomJasperDataSource implements JRDataSource {
MyObjectInMyTableClass chartEntry;
Iterator iterator;
ArrayList chartEntries;
public CustomJasperDataSource(ArrayList chartEntries) {
this.chartEntries = chartEntries;
iterator = chartEntries.iterator();
}
public Object getFieldValue(JRField jrField) throws JRException {
if (jrField.getName().equalsIgnoreCase("myObjectAttribute1")) {
return chartEntry.getMyObjectAttribute1();
}
if (jrField.getName().equalsIgnoreCase("myObjectAttribute2")){
return chartEntry.getMyObjectAttribute2();
}
...
return null;
}
public boolean next() throws JRException {
boolean hasNext = iterator.hasNext();
if (hasNext) {
chartEntry = (MyObjectInMyTableClass) iterator.next();
}
return hasNext;
}
}
Invoking Jasper
This is called when you press some swing button:
protected void jButtonCreatePdfActionPerformed(ActionEvent e) {
JasperPrint jp = generateReport(myArrayListOfData);
JasperViewer jasperViewer = new JasperViewer(jp, false);
jasperViewer.setVisible(true);
}
And this is how generateReport method looks like:
private JasperPrint generateReport(ArrayList data) {
JasperPrint jasperPrint = null;
String templateName = "MyTemplateName.jasper";
try {
jasperPrint = JasperFillManager.fillReport(templateName,
new HashMap(), new CustomJasperDataSource(data));
} catch (JRException e) {
doSOmeExcCatching();
}
return jasperPrint;
}