This section will contain a list of solutions to common problems. It will also serve as base for those who want to convert their reports manually and will list of features known to work in JFreeReport 0.8 can be achieved in the new system.
How to load a report
Loading the report definition from an XML file hasnt changed much.
Instead of using the ReportFactory
, we now use a centralized ResourceManager
. That resource manager handles all load-operations. It keeps track of changes to the underlying files and tries to apply as much caching as possible. The resource-manager is contained in LibLoader.
Code Block |
---|
ResourceManager manager = new ResourceManager();
manager.registerDefaults();
|
The resource manager provides resource-objects, which grant access to some additional meta-data. The first parameter of the 'create' operation is the source object. This can be anything, like an URL, a File (or a string representation of those) or a byte containing the raw-data. Resources stored on the classpath can be accessed using the String "res:/package/file.name".
Code Block |
---|
Resource res = manager.createDirectly(source, JFreeReport.class);
|
In most cases, however, we are interested in the result of the loading, which can be accessed like this.
Code Block |
---|
final JFreeReport report = (JFreeReport) res.getResource();
|
If the report definition contained references to StyleSheets or SQL-Datasources, then these referenced resources have been loaded as well. The report object can be fed directly into the preview components.
How to show a print preview
I assume that you already have a fully initialized JFreeReport-object. Simply create either a PreviewDialog or a PreviewFrame, set the report, and show the report. The dialog and frame can be reused (although noone will be hurt if new dialogs are created for each report).
The preview components have moved from package 'org.jfree.report.modules.gui.base' (in JFreeReport 0.8.x) to 'org.jfree.report.modules.gui.swing.preview'.
JFreeReport does no longer work directly with the JFreeReport object. All configuration and datasource properties are now accessed using a generic interface called 'ReportJob'. The ReportJob holds all information that are neccessary to process a report.
Code Block |
---|
JFreeReport report; // from the parser ..
// create a report-job
ReportJob job = new DefaultReportJob (report);
final PreviewDialog frame = new PreviewDialog();
frame.setReportJob(reportJob);
frame.pack();
RefineryUtilities.positionFrameRandomly(frame);
frame.setVisible(true);
|
How to assign DataSources to an report
When using SQL-DataSources in conjunction with the XML definitions, the parser will already connect the report definition with the datasource The Quadrant-Demo uses SQL-Databases, for example:
Code Block |
---|
<report:report xmlns="http://www.w3.org/1999/xhtml"
xmlns:report="http://jfreereport.sourceforge.net/namespaces/reports/flow">
<report:datasource href="quad.sqlds"/>
<report:query>default</report:query>
..
</report:report>
|
The 'quad.sqlds' is yet another XML file:
Code Block |
---|
<sql-datasource
xmlns="http://jfreereport.sourceforge.net/namespaces/datasources/sql"
xmlns:html="http://www.w3.org/1999/xhtml">
<connection>
<driver>org.hsqldb.jdbcDriver</driver>
<url>jdbc:hsqldb:./sql/sampledata</url>
<properties>
<property name="user">sa</property>
<property name="pass"></property>
</properties>
</connection>
<query name="default">
SELECT
QUADRANT_ACTUALS.REGION,
QUADRANT_ACTUALS.DEPARTMENT,
QUADRANT_ACTUALS.POSITIONTITLE,
QUADRANT_ACTUALS.ACTUAL,
QUADRANT_ACTUALS.BUDGET,
QUADRANT_ACTUALS.VARIANCE
FROM
QUADRANT_ACTUALS
ORDER BY
REGION, DEPARTMENT, POSITIONTITLE
</query>
</sql-datasource>
|
But not everyone uses SQL. To have a generic fallback and as we successfully used those in the past, we also support Swing-TableModels as inputsources. As stated in the documentation, the primitive Swing-Table-DataSources are not parametrizable, so there is no sane chance to get master detail-reports up and running with the default TableReportDataFactory
implementation.
Code Block |
---|
JFreeReport report; // from the parser ..
TableModel model; //
final DefaultReportJob job = new DefaultReportJob(report);
final TableReportDataFactory dataFactory = new TableReportDataFactory("default", tableModel);
job.setDataFactory(dataFactory);
|
The name the Table-Model is registered with the report-data-factory must be the same as the query-name used in the report-object.