Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: PrintJobListener Always Returns printJobNoMoreEvents

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default PrintJobListener Always Returns printJobNoMoreEvents

    Hi,

    In my Java application I print some text and afterwards I want to get the status of the job using the example in Determining When a Print Job Has Finished | Example Depot , however I always get the status "printJobNoMoreEvents" either the print operation has succeeded or not. I used OKI dot printer, HP laser printer and EPSON TM-U295 , but the results is the same for all of them.

    By the way, I observe that Windows gets the status of the job correctly, namely it gives the error status if the job is not printed properly. Therefore I guess it may be something related with PrintJobListener interface. What should I do?

    Thanks in advance.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: PrintJobListener Always Returns printJobNoMoreEvents

    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Jun 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: PrintJobListener Always Returns printJobNoMoreEvents

    Sorry, I am new to java forums and I was not aware of the fact that posting the same question to multiple forums is considered as wrong. I will be careful about that. However, this problem is very important to me and I will be grateful if some useful answer is supplied.

    Thanks in advance.

  4. #4
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: PrintJobListener Always Returns printJobNoMoreEvents

    Quote Originally Posted by Basak View Post
    ... I always get the status "printJobNoMoreEvents" either the print operation has succeeded or not.
    Can you post up your implementation of the PrintJobWatcher? The example you link to treats all print job events the same way, so I'm curious to know how you determine that you only get "printJobNoMoreEvents". Are you checking the PrintJobEvent object itself?

  5. #5
    Junior Member
    Join Date
    Jun 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: PrintJobListener Always Returns printJobNoMoreEvents

    Here is my code:

    public class Printer implements PrintJobListener
    {
     
        private String printJobStatus = null;
        private DocPrintJob docpj ;
     
     
     
        public String print(String printerName, String printString){
     
            PrintService printService = null;
            String name = "";
            PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null);
     
            for (PrintService ps : printServices) {
                PrintServiceAttribute attr = ps.getAttribute(PrinterName.class);
                name = ((PrinterName) attr).getValue();
                if (name.equalsIgnoreCase(printerName)) {
                    printService = ps;
                    break;
                }
            }
     
            if (printService == null) {
                return "Printer is not available..";
            }
     
            StringBuffer printBuffer = new StringBuffer(50);
     
            writeMessage(printBuffer, "STRING: " + printString, 1);
     
            docpj = printService.createPrintJob();
            docpj.addPrintJobListener(this);
     
     
            DocFlavor df = DocFlavor.BYTE_ARRAY.AUTOSENSE;
            Doc doc = new SimpleDoc(printBuffer.toString().getBytes(), df, null);
     
            try
            {
            	 docpj.print(doc, null);
     
     
                // Wait till we know the status of our print job
                while(printJobStatus == null)
                {
                    // Wait for 1 second before checking to see if the job is complete.
                    try
                    {
                        Thread.sleep(1000);
                    }
                    catch (InterruptedException ignore)
                    {
                        ignore.printStackTrace();
                    }
                }
            }
            catch (PrintException exception)
            {
                exception.printStackTrace();
                return "Some exception thrown..";
            }
     
     
            return printJobStatus;
        }
     
        public void writeMessage(FileWriter fw, String message, int repeatCount) {
            for (int i = 0; i < repeatCount; i++) {
     
                try {
                    fw.write(message);
                    fw.write(13);
                    fw.flush();
                } catch (IOException e) {
                }
            }
        }
     
        public void writeMessage(StringBuffer sb, String message, int repeatCount) {
            for (int i = 0; i < repeatCount; i++) {
                sb.append(message);
                char c = 13;
                sb.append(c);
            }
        }
     
     
        @Override
        public void printJobCompleted(PrintJobEvent pje)
        {
            printJobStatus = "printJobCompleted";
        }
     
        @Override
        public void printJobNoMoreEvents(PrintJobEvent pje)  
        {
            printJobStatus = "printJobNoMoreEvents";
        }
     
        @Override
        public void printDataTransferCompleted(PrintJobEvent pje)
        {
        	printJobStatus = "printDataTransferCompleted";
        }
     
        @Override
        public void printJobCanceled(PrintJobEvent pje)
        {
            printJobStatus = "printJobCanceled";
        }
     
        @Override
        public void printJobFailed(PrintJobEvent pje)
        {
            printJobStatus = "printJobFailed";
     
        }
     
        @Override
        public void printJobRequiresAttention(PrintJobEvent pje)
        {
            printJobStatus = "printJobRequiresAttention";
        }
    }

    In this code print() function always returns "printJobNoMoreEvents" as the printJobStatus string.

    Thank you.

  6. #6
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: PrintJobListener Always Returns printJobNoMoreEvents

    OK, your code isn't allowing for multiple PrintJobEvents - remember that the NoMoreEvents notification should always be sent when the print service finishes a job. Your listener code effectively returns only the last event it receives, because the printJobStatus variable is overwritten by every event that arrives, so you're always going to see NoMoreEvents because that's always the last event. Try sending the printJobStatus to the console in every PrintJobEvent handler method to see what events are sent. You could use a list to hold the events received for a job, instead of a single printJobStatus variable.

    However, it's possible (but unlikely) that the print service is at fault. The API docs say that the 'NoMoreEvents' notification is always sent:

    "Not all print services may be capable of delivering interesting events, or even telling when a job is complete. This message indicates the print job has no further information or communication with the print service. This message should always be delivered if a terminal event (completed/failed/canceled) is not delivered. For example, if messages such as JOB_COMPLETE have NOT been received before receiving this message, the only inference that should be drawn is that the print service does not support delivering such an event".

Similar Threads

  1. Question with PrintJobListener
    By inxis in forum Java Theory & Questions
    Replies: 1
    Last Post: November 9th, 2011, 02:44 AM
  2. Javascript function returns NaN
    By jai in forum Other Programming Languages
    Replies: 2
    Last Post: April 19th, 2011, 02:31 AM
  3. returns all suggestions
    By mario_tim in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 26th, 2011, 04:51 AM
  4. Space Monkey Returns
    By SPACE MONKEY in forum Member Introductions
    Replies: 1
    Last Post: February 17th, 2011, 09:59 AM
  5. How web service returns xml?
    By nikos in forum Web Frameworks
    Replies: 2
    Last Post: October 7th, 2010, 12:50 PM