Java Reference
In-Depth Information
Now you have the ability to set attributes on any of the tabs in the dialog, and the attributes are stored
in the
PrintAttr
member of the
SketcherFrame
class that you passed to the
printDialog()
method. Be-
cause you also pass this reference to the
print()
method for the
PrinterJob
object, the print request is
executed using these attributes. This is accomplished by passing a
PageFormat
object to the
Printable
ob-
ject, which prints a page that has its size, orientation, and other attributes set from the print request attributes
defined by
PrintAttr
. You can see that the page count has been set to 1 by default in this dialog. You can
set attributes related to a print job and store them in the
PrintRequestAttributeSet
object that you pass
to the
printDialog()
method. Let's explore that a little further.
Setting Print Request Attributes Programmatically
Print request attributes are specifications of the kinds of options displayed in the dialog you 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
PrintRequestAttrib-
ute
interface, and the
javax.print.attributes.standard
package defines a series of classes for stand-
ard print request attributes, as well as classes for other types of print attributes. There is a large number of
standard classes for print request attributes, and I don't have the space to go into the details of them all here.
So I'm just picking one to show how you can query and set them.
All the classes that implement the
PrintRequestAttribute
interface are identified in the interface doc-
umentation. You can use the
Copies
class in the
javax.print.attributes.standard
package that speci-
fies the number of printed copies to be produced.
You add an instance of the
Copies
class to your
PrintAttr
object to specify the number of copies to be
printed, and this is displayed by the print dialog. You can create an object specifying the number of copies
to be produced by extending the code relating to printer setup in the
SketcherFrame
constructor, like this:
// Set up the printer and page format objects
printJob = PrinterJob.getPrinterJob(); // Get a printing object
pageFormat = printJob.defaultPage(); // Get the page format
printer = printJob.getPrintService(); // Get the default printer
Copies twoCopies = new Copies(2);
if (printer.isAttributeCategorySupported(twoCopies.getCategory())) {
printAttr.add(twoCopies);
}
The argument to the
Copies
class constructor specifies the number of copies to be produced. Our object
specifies just two copies, but you can go for more if you have the paper, the time, and the inclination.
Before you add this object to the print request attribute set, you verify that the printer does actually sup-
port the production of multiple copies. Obviously, it only makes sense to set an attribute for a printer that has
the appropriate capability — you won't be able to print in color on a monochrome printer, for instance. You
can call the
isAttributeCategorySupported()
method for the
PrintService
object that you obtained
from the
PrinterJob
object to do this.
The
isAttributeCategorySupported()
method requires an argument of type
Class
to identify the
attribute category that you are querying, and you obtain this by calling the
getCategory()
method for
the
Copies
object. If the attribute is supported, you add the
twoCopies
object to the set encapsulated by
printAttr
by calling its
add()
method.
You should add an
import
statement for the
Copies
class to
SketcherFrame.java
: