Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Trouble Printing with java 1.4!

 
Andrew Keidel
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! Below is my code (it's short) and the output/error I get. Any ideas wtf is going on???

CODE:
public void print() throws PrinterException {
PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.setPrintable(this, this.pageFormat);
PrintRequestAttributeSet pset = new HashPrintRequestAttributeSet();
pset.add(new PrinterResolution(760, 1110, ResolutionSyntax.DPI));
DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
DocAttributeSet aset = new HashDocAttributeSet();
PrintService[] pservices = PrintServiceLookup.lookupPrintServices(flavor, null);
for (int i = 0; i < pservices.length; i++) {
System.out.println(i + ": " + pservices[i].getName() + " |||| "
+ pservices[i].getSupportedDocFlavors() + " |||| "
+ pservices[i].hashCode());
}
PrintService pservice = pservices[1];
System.out.println("looked up service: " + pservice);
if (pservice != null) {
System.out.println("got service");
DocPrintJob pj = pservice.createPrintJob();
Doc doc = new SimpleDoc(this, flavor, aset);
try {
System.out.println("trying to print");
pj.print(doc, null);
} catch (Exception e) {
e.printStackTrace();
}
}
}

OUTPUT & ERROR:
0: \\regs\HP LaserJet 4050 Series PCL |||| [Ljavax.print.DocFlavor;@c791b9 |||| 1273058671
1: ZTC Z4M-200dpi |||| [Ljavax.print.DocFlavor;@b15692 |||| 775228905
2: Symantec Fax Starter Edition |||| [Ljavax.print.DocFlavor;@aa9f99 |||| -1486058539
3: HP LaserJet 1200 |||| [Ljavax.print.DocFlavor;@d42d08 |||| 1922980968
looked up service: Win32 Printer : ZTC Z4M-200dpi
got service
trying to print
java.lang.ArrayIndexOutOfBoundsException
at sun.print.Win32PrintService.getDefaultAttributeValue(Win32PrintService.java:786)
at sun.print.Win32PrintJob.printableJob(Win32PrintJob.java:413)
at sun.print.Win32PrintJob.print(Win32PrintJob.java:342)
at com.mixonic.admin.print.ShipLabel.print(ShipLabel.java:146)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic