Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Retries is Java printing?

Howard Rosenblum
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you run the following code(from, both pages are called twice

class PrintObject implements Printable
public int print (Graphics g, PageFormat f, int pageIndex)
System.out.println("Page "+pageIndex);
Graphics2D g2 = (Graphics2D) g; // Allow use of Java 2 graphics on
// the print pages :

// A rectangle that shows the printable area of the page, allowing
// for margins all round. To be drawn on the first page (index = 0).
Rectangle2D rect = new Rectangle2D.Double(f.getImageableX(),

// A simple circle to go on the second page (index = 1).
Ellipse2D circle = new Ellipse2D.Double(100,100,100,100);

switch (pageIndex)
case 0 : g2.setColor(; // Page 1 : print a rectangle
case 1 : g2.setColor(; // Page 2 : print a circle
default: return NO_SUCH_PAGE; // No other pages

public static void main (String[] args)
// Create an object that will hold all print parameters, such as
// page size, printer resolution. In addition, it manages the print
// process (job).
PrinterJob job = PrinterJob.getPrinterJob();

// It is first called to tell it what object will print each page.
job.setPrintable(new PrintObject());

// Then it is called to display the standard print options dialog.
if (job.printDialog())
// If the user has pressed OK (printDialog returns true), then go
// ahead with the printing. This is started by the simple call to
// the job print() method. When it runs, it calls the page print
// object for page index 0. Then page index 1, 2, and so on
// until NO_SUCH_PAGE is returned.
try { job.print(); }
catch (PrinterException e) { System.out.println(e); }

I don't know why it would do this in such a simple case but the real problem is that we have code that prints a large JTable, and it retries printing over 500 times before it succeeds. Even for a relatively small JTable it retries 11 times. We are using java.awt.print.PrintJob, turned off double buffering and testing using CutePDF on a machine that has 2G RAM and 1G Heap(-Xmx1000m)

Does anyone know what would cause so many retries?

Thank you for any help
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic