Java Reference
In-Depth Information
As you can see, with my printer I can select the paper size and the source tray. I can also set the margin
sizes as well as select portrait or landscape orientation. When the dialog is closed normally with the OK
button, the method returns a new
PageFormat
object that incorporates the values set by the user in the dia-
log. If the Cancel button is used to close the dialog, the original reference that was passed as an argument
is returned. The printing operations that are initiated in the
actionPerformed()
method in the
FileAction
class use the updated
PageFormat
object. You achieve this by passing
pageFormat
as the second argument
to the overloaded
setPrintable()
method with two parameters. The first argument is the object that im-
plements the
Printable
interface and thus carries out the printing, and the second is the
PageFormat
object
that is to be used.
Using the Java Print Dialog
The overloaded version of the
printDialog()
method that I sidestepped earlier generates a Java-based print
dialog instead of using the native dialog. This method requires a single argument of type
PrintRequestAt-
tributeSet
. This interface type is defined in the
javax.print.attribute
package and declares methods
for adding attributes relating to a print request to a set. These attributes specify things such as the number of
copies to be printed, the orientation of the image on the paper, or the media or media tray to be selected for
the print job. The
HashPrintRequestAttributeSet
class that is defined in the same package implements
this interface and encapsulates a set of print request attributes stored as a hash map. It is useful if you can
define a set of attributes that have some persistence in Sketcher so they can be carried forward from one
print request to the next. You can add a member to store an initially empty set of print request attributes to
the
SketcherFrame
class like this:
private HashPrintRequestAttributeSet printAttr = new
HashPrintRequestAttributeSet();