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 net.sf.jasperreports.engine.data.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...