How to display print progress dialog in Swing?

If I do this in my class that implements Printable,

PrinterJob job = PrinterJob.getPrinterJob(); job.setJobName( /* some name */ ); Book book = new Book(); book.append(this, new PageFormat()); job.setPageable(book); boolean ok = job.printDialog(); if (ok) { try { job.print(); } catch (PrinterException ex) { ex.printStackTrace(); } } 

it works, but my Printable class gets print() twice. (presumably for assigning resources by the printer driver)

How can I display the progress bar correctly? In my case, I know how many sub-printing tasks I need to complete, but if I try to display it, the progress bar will start from beginning to end twice, which is terrible feedback for the user. And I would prefer not to display an indefinite progress bar ....

What should I do?


Explanation: I print a bunch of stories on the page. They are somewhat confusing and take time to print, so I could know that there are 10 objects to render, so I would like my progress bar to be from 0 to 10 and increase it after I have displayed each of 10 objects, (Or, even better, go from 0 to 20, assuming I somehow know that the page was done twice.)

+4
source share
2 answers

I abandoned the idea of โ€‹โ€‹trying to predict or distinguish # calls from print ().

Answer to

@Atreys was promising, but the only difference I could use without mirroring or importing private sun.* sun.print.PeekGraphics that sun.print.PeekGraphics implements java.awt.image.ImageObserver , whereas sun.awt.windows.WPathGraphics no. (getClass (). getName () is considered a reflection in my book)

As a result, I used a progress bar for each individual call to Printable.print() , for example. reset progress at the beginning and advance it for each substep print. A.

So that users do not complain that the progress bar shows progress twice, I have a label that has been changed to "Pass # {k}" whenever print () is called, where {k} is the counter that increments every time.


ps The correct way to cancel a print job from Printable.print() bit more complicated. Throwing PrintAbortException does not work correctly because it ends the print () call, but the printer driver continues to work despite the fact that Printable javadoc says the following:

If the Printable object aborts the print job, it throws a PrinterException.

Instead, you should apply the Graphics () parameter of the Graphics () method as a PrintGraphics object, and then call PrintGraphics.getPrinterJob (). cancel ():

 if (graphics instanceof PrinterGraphics) { PrinterGraphics pg = (PrinterGraphics) graphics; pg.getPrinterJob().cancel(); } else { // this is kind of an unexpected circumstance // not sure if we should do this or throw IllegalStateException() PrinterAbortException pae = new PrinterAbortException(); pae.initCause(/* something */); throw pae; } 
+1
source

Looking at the arguments provided to the printing method, the first time it is called (at least for me), the graphic is a PeekGraphics object, and the second time it is WPathGraphics. On my system, RasterPrinterJob creates PeekGraphics to get information about a print job before setting up graphics for a real print job. A.

Depending on how you update your progress bar, you can simply check that the graphic argument is not PeekGraphics before giving it a tick.

+3
source

All Articles