Binding Data to a Report Template
If data binding is not specified in a template, the data must be supplied to the report elements at runtime. Associating a data object with an element using the unique element ID performs the runtime data binding. The ID is set in the Designer property dialog and is also returned from the 'add' element function.
The main method for data binding is setElement(). The setElement() method takes an element ID and a value parameter. The element ID must be unique for each element on a report.
Which data types are acceptable for the element depends on the type of element referred to by the element ID. The following element types and data types are supported by the setElement() method:
Table 5. Data Types Associated with an Element
Element Type |
Data Types |
Section |
TableLens, Object[ ][ ] |
Table |
TableLens, Object[ ][ ] |
Form |
FormLens, Object[ ][ ] |
Text |
String, TextLens |
TextBox |
String, TextLens |
Painter |
Image, Component, Painter |
Subreport |
ReportSheet |
Example: setElement
An example of using setElement():
ReportSheet report = ...; // load in a template
// set the contents of 'table1'
report.setElement("table1", new JTableLens(jTable1));
report.print(...);
If the ID specified in the setElement() method does not exist in the report template, a NoSuchElementException is thrown. If the data type does not match the element type, an IllegalArgumentException is thrown. Since report templates are normally static and would not change from development to deployment, these types of errors are normally very easy to detect.
Since a report template can store all the static data and attributes in a report, it is generally only necessary to call the setElement() method a few times (and often, just once) when setting up a report. This design greatly reduces the programming involved in creating reports and removes the tedious process of laying out report elements by trial and error.