public static PrintService printDialog(GraphicsConfiguration gc,
int x, int y,
PrintService[] services,
PrintService defaultService,
DocFlavor flavor,
PrintRequestAttributeSet attributes)
throws HeadlessException
{
int defaultIndex = -1;
if (GraphicsEnvironment.isHeadless()) {
throw new HeadlessException();
} else if ((services == null) || (services.length == 0)) {
throw new IllegalArgumentException("services must be non-null " +
"and non-empty");
} else if (attributes == null) {
throw new IllegalArgumentException("attributes must be non-null");
}
if (defaultService != null) {
for (int i = 0; i < services.length; i++) {
if (services[i].equals(defaultService)) {
defaultIndex = i;
break;
}
}
if (defaultIndex < 0) {
throw new IllegalArgumentException("services must contain " +
"defaultService");
}
} else {
defaultIndex = 0;
}
// For now we set owner to null. In the future, it may be passed
// as an argument.
Window owner = null;
Rectangle gcBounds = (gc == null) ? GraphicsEnvironment.
getLocalGraphicsEnvironment().getDefaultScreenDevice().
getDefaultConfiguration().getBounds() : gc.getBounds();
ServiceDialog dialog;
if (owner instanceof Frame) {
dialog = new ServiceDialog(gc,
x + gcBounds.x,
y + gcBounds.y,
services, defaultIndex,
flavor, attributes,
(Frame)owner);
} else {
dialog = new ServiceDialog(gc,
x + gcBounds.x,
y + gcBounds.y,
services, defaultIndex,
flavor, attributes,
(Dialog)owner);
}
Rectangle dlgBounds = dialog.getBounds();
// get union of all GC bounds
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] gs = ge.getScreenDevices();
for (int j=0; j
gcBounds =
gcBounds.union(gs[j].getDefaultConfiguration().getBounds());
}
// if portion of dialog is not within the gc boundary
if (!gcBounds.contains(dlgBounds)) {
// put in the center relative to parent frame/dialog
dialog.setLocationRelativeTo(owner);
}
[color=red] dialog.show();[/color]
if (dialog.getStatus() == ServiceDialog.APPROVE) {
PrintRequestAttributeSet newas = dialog.getAttributes();
Class dstCategory = Destination.class;
Class amCategory = SunAlternateMedia.class;
Class fdCategory = Fidelity.class;
if (attributes.containsKey(dstCategory) &&
!newas.containsKey(dstCategory))
{
attributes.remove(dstCategory);
}
if (attributes.containsKey(amCategory) &&
!newas.containsKey(amCategory))
{
attributes.remove(amCategory);
}
attributes.addAll(newas);
Fidelity fd = (Fidelity)attributes.get(fdCategory);
if (fd != null) {
if (fd == Fidelity.FIDELITY_TRUE) {
removeUnsupportedAttributes(dialog.getPrintService(),
flavor, attributes);
}
}
}
return dialog.getPrintService();
}
ServiceUI.printDialog 就是显示打印对话框,当你确认之后返回dialog.getPrintService();
打印时必须
if (services.length > 0) {
* PrintService service = ServiceUI.printDialog(null, 50, 50,
* services, services[0],
* null,
* attributes);
* if (service != null) {
* ... print ...
* }
* }
即当
if (service != null) {
... print ...
}
可以加上else表示无法打印
你直接打印就不显示对话框了
PrintService ps = pss[0];
//PrintService ps = PrintServiceLookup.lookupDefaultPrintService() ;
DocPrintJob job = ps.createPrintJob();
DocAttributeSet das = new HashDocAttributeSet();
Doc doc = new SimpleDoc(fi, df, das);
if (ps != null) {
job.print(doc, pras);
fi.close();
}
2012年12月16日 15:26