Java Reference
In-Depth Information
Now we have the ability to set attributes on any of the tabs in the dialog and the attributes will be stored
in the
PrintAttr
member of the
SketchFrame
class that we passed to the
printDialog()
method. Since we also pass this reference to the
print()
method for the
PrinterJob
object, the
print request will be executed using these attributes. This is accomplished by passing a
PageFormat
object to our
Printable
object that prints a page that has its size, orientation, and other attributes set
from the print request attributes defined by
PrintAttr
. Note that although you can set print attributes
in the dialog, they may be overridden by your code that does the printing. For instance, you render the
sketch in color by setting the color for each element before you draw it, so selecting monochrome for
the color appearance tab will have no effect. Similarly, the code determines whether to use portrait or
landscape orientation, so the selection in the dialog for this will be overridden too.
Setting Print Request Attributes Programmatically
Print request attributes are specifications of the kinds of options displayed in the dialog we just saw. They
specify things like the number of copies, whether printing is in color or monochrome, the page margin sizes,
and so on. Each print request attribute is identified by a class that implements the
PrintRequestAttribute
interface, and the
javax.print.attributes.standard
package defines
a series of classes for standard print request attributes, as well as classes for other types of print attributes.
There are a large number of standard classes for print request attributes and we don't have the space to go
into the details of them all here so we will just pick one to show how you can query and set them.
All the classes that implement the
PrintRequestAttribute
interface are identified in the interface
documentation. We will use the
Copies
class in the
javax.print.attributes.standard
package that specifies the number of printed copies to be produced.