Hi Everyone

I am working one swing application which need to print on some specific
printer on network. Method which I am using is given below. In this
method i am passing the printer name as parameter and string which is
to be printed is present in the class member [b]printString[/b].

        public boolean print(String printerName) {
                boolean result = false;
                try {
                        ByteArrayInputStream bais = new 
ByteArrayInputStream(printContent.getBytes());
                        PrintService[] services = 
PrinterJob.lookupPrintServices();
                        String [] printerNameList = new String[services.length];
                        for(int count = 0; count < services.length; count++) {
                                printerNameList[count] = 
services[count].getName();
                                if 
(printerNameList[count].equalsIgnoreCase(printerName)) {
                                        DocPrintJob job = 
services[count].createPrintJob();
                                        DocFlavor flavor = 
DocFlavor.BYTE_ARRAY.AUTOSENSE;
                                        Doc doc = new 
SimpleDoc(printContent.getBytes(), flavor, null);
                                        PrintRequestAttributeSet attributes = 
new HashPrintRequestAttributeSet();
                                        attributes.add(new Copies(1));
                                        
                                        PrintJobWatcher pjDone = new 
PrintJobWatcher(job);

                                        job.print(doc, attributes);
                                        
                                        pjDone.waitForDone();
                                        
                                        bais.close();
                                        
                                        result = true;
                                        break;
                                }
                        }
                }
                catch(PrintException ex) {
                        String message = "Error on while printing the content 
on the printer : "  + ex.getMessage();
                        System.out.println(message);
                        ApplicationLogger.getInstance().error(message);
                }
                catch(Exception ex) {
                        String message = "Error on while printing the content : 
"  + ex.getMessage();
                        System.out.println(message);
                        ApplicationLogger.getInstance().error(message);
                }
                return result;
        }

Here is the code for PrintJobWatcher 

        class PrintJobWatcher {
        // true iff it is safe to close the print job's input stream
        boolean done = false;
    
        PrintJobWatcher(DocPrintJob job) {
            // Add a listener to the print job
            job.addPrintJobListener(new PrintJobAdapter() {
                public void printJobCanceled(PrintJobEvent event) {
                    allDone();
                }
                public void printJobCompleted(PrintJobEvent event) {
                    allDone();
                }
                public void printJobFailed(PrintJobEvent event) {
                    allDone();
                }
                public void printJobNoMoreEvents(PrintJobEvent event) {
                    allDone();
                }
                void allDone() {
                    synchronized (PrintJobWatcher.this) {
                        done = true;
                        PrintJobWatcher.this.notify();
                    }
                }
            });
        }
        public synchronized void waitForDone() {
            try {
                while (!done) {
                    wait();
                }
            }
            catch (InterruptedException e) {
            }
        }
    }

But my string is not being printed. Please tell where I am doing mistake.

Thanks in advance
Shashwat
[Message sent by forum member 'shashwat_anand' (shashwat_anand)]

http://forums.java.net/jive/thread.jspa?messageID=333974

===========================================================================
To unsubscribe, send email to lists...@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST".  For general help, send email to
lists...@java.sun.com and include in the body of the message "help".

Reply via email to