Documente Academic
Documente Profesional
Documente Cultură
Event handler
and listeners
,swing and
jframe ,panels,
drivers plugin
Testing of java panel
and Source code
with testing of
software with new
drivers
Software
maintenance New
panel added with
swing componants
Job control Creating a print job, associating it with a printer, specifying the number of copies,
and user print dialog interaction.
Page Imaging Drawing content to a page, and managing content that spans pages (pagination).
First create the printer job. The class representing a printer job and most other related classes is located in
the java.awt.print package.
import java.awt.print.*;
PrinterJob job = PrinterJob.getPrinterJob();
Next provide code that renders the content to the page by implementing the Printable interface.
class HelloWorldPrinter
implements Printable { ... }
...
job.setPrintable(new HelloWorldPrinter());
An application typically displays a print dialog so that the user can adjust various options such as number of
copies, page orientation, or the destination printer.
if (doPrint) {
try {
job.print();
} catch (PrinterException e) {
// The job did not successfully
// complete
}
}
The PrinterException will be thrown if there is problem sending the job to the printer. However, since
the PrinterJob.print method returns as soon as the job is sent to the printer, the user application cannot
detect paper jams or paper out problems. This job control boilerplate is sufficient for basic printing uses.
The Printable interface has only one method:
import java.awt.print.*;
import java.awt.*;
public class HelloWorldPrinter
implements Printable {
public int print(Graphics g, PageFormat pf, int page)
throws PrinterException {
// We have only one page, and 'page'
// is zero-based
if (page > 0) {
return NO_SUCH_PAGE;
}
// User (0,0) is typically outside the
// imageable area, so we must translate
// by the X and Y values in the PageFormat
// to avoid clipping.
Graphics2D g2d = (Graphics2D)g;
g2d.translate(pf.getImageableX(), pf.getImageableY());
// Now we perform our rendering
g.drawString("Hello world!", 100, 100);
// tell the caller that this page is part
// of the printed document
return PAGE_EXISTS;
}
}
The complete code for this example is in HelloWorldPrinter.java.
Sending a Graphics instance to the printer is essentially the same as rendering it to the screen. In both cases
you need to perform the following steps:
To draw a test string is as easy as the other operations that were described for drawing to
a Graphics2D.
Printer graphics have a higher resolution, which should be transparent to most code.
The print() method may be called with the same page index multiple times until the document
is completed. This feature is applied when the user specifies attributes such as multiple copies with
collate option.
The PageFormat's imageable area determines the clip area. Imageable area is also important in
calculating pagination, or how to span content across printed pages, since page breaks are
determined by how much can fit on each page.
Note: A call to the print() method may be skipped for certain page indices if the user has
specified a different page range that does not involve a particular page index
An application displays a print dialog when the user presses a button related to the print command, or chooses
an item from the print menu. To display this dialog, call theprintDialog method of the PrinterJob class:
PrinterJob pj = PrinterJob.getPrinterJob();
...
if (pj.printDialog()) {
try {pj.print();}
catch (PrinterException exc) {
System.out.println(exc);
}
}
...
This method returns true if the user clicked OK to leave the dialog, and false otherwise. The user's choices
in the dialog are constrained based on the number and format of the pages that have been set to
the PrinterJob.
The printDialog method in the above code snippet opens a native print dialog.
The PrintDialogExample.java code example shows how to display a cross-platform print dialog.
You can change the page setup information contained in the PageFormat object by using the page setup
dialog.
To display the page setup dialog, call the pageDialog method of the PrinterJob class.
PrinterJob pj = PrinterJob.getPrinterJob();
PageFormat pf = pj.pageDialog(pj.defaultPage());
The page setup dialog is initialized using the parameter passed to pageDialog. If the user clicks the OK
button in the dialog, the PageFormat instance will be created in accordance with the users selections, and
then returned. If the user cancels the dialog, pageDialog returns the original unchanged PageFormat.
Usually the Java 2D Printing API requires an application to display a print dialog, but in sometimes it's possible
to print without showing any dialog at all. This type of printing is called silent printing. It may be useful in
specific cases, such as, when you need to print a particular database weekly report. In the other cases it is
always recommended to inform the user when a print process is starting.
The page format may not be disclosured until printing occurs. Since if the user selected a
landscape mode in the print dialog, then this setting needs to be accounted for.
The PageFormat object passed into the print() method provides this information.
The page break positions are used as represented in the following code fragment:
The Java Print Service (JPS) API extends the current Java 2D printing features to offer the following
functionality:
Application discovers printers that cater to its needs by dynamically querying the printer capabilities.
Third parties can plug in their own print services with the Service Provider Interface, which print
different formats, including Postscript, PDF, and SVG.
The javax.print package provides the principal classes and interfaces for the Java Print Service API. It
enables client and server applications to:
Submit print jobs to services that support the document type to be printed.
lookupStreamPrintServiceFactories(
flavor, psMimeType);
Attribute Definitions
The javax.print.attribute and javax.print.attribute.standard packages define print
attributes which describe the capabilities of a print service, specify the requirements of a print job, and track the
progress of the print job.
For example, if you would like to use A4 paper format and print three copies of your document you will have to
create a set of the following attributes implementing thePrintRequestAttributeSet interface:
PrintRequestAttributeSet attr_set =
new HashPrintRequestAttributeSet();
attr_set.add(MediaSize.ISO_A4);
attr_set.add(new Copies(3));
Then you must pass the attribute set to the print job's print method, along with the DocFlavor.
Chooses a DocFlavor.
2.
3.
Locates a print service that can handle the print request as specified by the DocFlavor and the
attribute set.
4.
Creates a Doc object encapsulating the DocFlavor and the actual print data.
5.
6.
java.awt.Component.print(Graphics g);
java.awt.Component.printAll(Graphics g);
The following figure represents a simple user interface.
javax.swing.JTable.print();
javax.swing.text.JTextComponent.print();
These methods provide a full implementation of printing for their contents. An application doesn't need directly
create a PrinterJob object and implement the Printableinterface. The call of these methods displays a
print dialog and prints the component's data in accordance with the user's selections. There are also additional
methods which provide more options.