Java Reference
In-Depth Information
The main difference between this example and the previous one is that here we are
grouping our data objects in a java.util.ArrayList instead of an array. When
using JRBeanCollectionDataSource to populate our reports, we do not necessarily
need to use an ArrayList to populate our beans. Any class implementing java.
util.Collection will work just as well. JRBeanCollectionDataSource works like
the previous JRDataSource implementations we have seen; that is, it has a single
public constructor that takes a Collection of objects as its only argument. We can
then use the initialized JRBeanCollectionDataSource to fill the report. This is
accomplished by the call to JasperRunManager.runReportToPDFStream() from the
doGet() method in the preceding example.
TableModels as Datasources
In many client-side applications, data is displayed in tabular format. A common
requirement in many applications is to allow the user to print this tabular format
as a report.
JasperReports provides an implementation of the JRDataSource interface that
makes the task of generating reports from tabular format trivial for Swing
applications. The class in question is the
JRTableModelDataSource . This class takes a javax.swing.table.TableModel as
its only parameter. Since tables in Swing are populated via TableModels , all we need
to do to generate a report from a table is to pass the appropriate table's TableModel
as a parameter. The following example is a simple but complete Swing application
demonstrating this process:
package net.ensode.jasperbook;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import net.sf.jasperreports.engine.JRException;
Search WWH ::

Custom Search