How to print a report.
There are several approaches to code a report printing applications using the JetBee print library. It will depend on whether you want to use the Java print model, or an alternative print model provided by the JetBee print library. And it also depends on if you want to print under the foreground or a background thread.
If you have existing Java programs and want them to utilize the library, then a quick and easy way is to use the Java print mode. See How to print using the Java print model.
If you want to use the JetBee print model, please see How to print using the JetBee print model.
How to print using the Java print model.
The JetWriter class can also work with the Java Printable class easily. If you have existing programs that are using the Java Printable class, or if you want to continue to use the Java Printable class while taking advantages of the JetBee print library, you can do this:
class MyReport implements Printable {
…
}
JetWriter jw = new JetWriter();
boolean go = jw.jetPrintDialog();
if (go) {
jw.jetExePrintable(new MyReport()); // execute in foreground thread.
}
If you want to use a background thread to print the report, you need simply to use the jetRunPrintable() instead:
…
if (go) {
jw.jetRunPrintable(new MyReport()); // execute in a background thread.
}
How to print using the JetBee print model.
This can be easily achieved by using a JetWriter object:
JetWriter jw = new JetWriter();
Once you have a JetWriter object, you can print using the member functions of the writer. Or you may obtain a Graphics2D object with which all the powerful Graphics2D functions can be used to print the report. For example:
// optionally set the print job attributes here.
jw.jetOpenReport();
Graphics2D g = jw.jetGetGraphics2D();
g.drawString(“JetBee Sample 1 Report”, 220, 150);
g.drawLine(220, 152, 355, 152);
This method is a convenient way to code for reports that are relatively simplier and can be finished quickly. There is no need to code for other class or routines. However, program coded in this way will run in the foreground thread.
If it is needed to print a report in a background thread, or using the features provided by the JetReport or its derived classes, then you may consider to create a class to contain the codes that print the report.
Your class would be derived from the JetReport or one of its derived classes such as the JetGraphics2DReport class.
For example:
class ProductList extends JetGraphics2DReport {
public void jetPrint() throws JetException {
// codes for generating the report contents.
// …
}
}
Then, in your main program, create a JetWriter instance and pass an instance of your report class to the JetWriter with the function jetRunReport():
JetWriter jw = new JetWriter();
// optionally set the print job attributes here.
jw.jetRunReport(new ProductList());
Then, the writer will call the codes in your report class in a background thread to generate the report.
How to preview a report.
The preview service can be easily employed simply by activating it using a function:
JetWriter jw = new JetWriter();
…
jw.jetSetAutoPreview(true); // activate the preview service.
Alternatively, some constructors also allow the preview service to be set:
JetWriter jw = new JetWriter(true, false); // preview is activated; printer output is disabled.
Once activated, the library will automatically bring up a preview window when a few pages of the report are generated and ready to be shown. It does not need to wait for the entire report to be finished. This would be useful for reports that contain thousands of pages.
How to print AWT or Swing components.
The jetPrintComponent() family of functions can be used to print any AWT/Swing components. These functions allow the position and size of a component to be set. There are also specific functions to print JTable objects. For example:
JetWriter jw = new JetWriter();
jw.jetOpenReport();
JLabel mylabel = new JLable(“Description”);
jetPrintComponent(mylabel, 50, 100);
JTable mytable = new JTable(…);
jetPrintComponent(mytable, 100, 200, “80%”, “80%”);