Printing dialog shows 9999 pages to print

J

JavaRookie

When I attempt to print the Graphics contents of a swing component, the print
dialog shows to print 9999 pages. But it should contain only 1 page.
Can anyone tell me why is that happening? and how to fix it?

Thx a lot!!


PrinterJob job = PrinterJob.getPrinterJob();

job.setCopies(1);
job.setPrintable(new Printable() {
public int print(Graphics g, PageFormat pf, int pageIndex) {
aSwingComponent.printAll(g);
return Printable.PAGE_EXISTS;
}
});

// Then when I show the print dialog, it shows 9999 pages to print!!
if (job.printDialog()) {
try {
job.print();
}
catch (Exception e) { }
}
 
S

servee

Uzytkownik "JavaRookie said:
When I attempt to print the Graphics contents of a swing component, the print
dialog shows to print 9999 pages. But it should contain only 1 page.
Can anyone tell me why is that happening? and how to fix it?
Why do you want to fix it.
When the "print dialog" appear, you have options:
- print all
or
- print pages from: ... to: ...
or
-selected (mostly disabled)

the default options is print all (in your case: 1 page)
if you have to print more then 1 page, for example 10, and you wont to print
only pages 2,3,4, simply select print pages and fill from: 2, to: 4.
9999 is the default value for this option.

servee
 
K

K.Koper

Hi,

you should actually return Printable.NO_SUCH_PAGE when your print()
function gets called with a non existing page index (in your case a page
index of 1 or higher (it's zero based iirc)). The print system will not
magically know when to stop calling your print function ;-)

HTH,

Klaas
 
S

Shannon Hickey - Swing Team

Hi,

When I attempt to print the Graphics contents of a swing component, the print
dialog shows to print 9999 pages. But it should contain only 1 page.
Can anyone tell me why is that happening? and how to fix it?

This happens when you use a Printable to do your printing. At the time
the print dialog is shown, the printing system has no idea how many
pages there are to print. It can't know that until it has finished
printing them and you've returned Printable.NO_SUCH_PAGE (which you
haven't done in your code below by the way - hope you haven't tried
printing this already, it will print forever).

If you want to provide this information ahead of time, try using
Pageable instead of Printable.

Regards,
Shannon
 
G

Gerbrand van Dieijen

When I attempt to print the Graphics contents of a swing component, the print
dialog shows to print 9999 pages. But it should contain only 1 page.
Can anyone tell me why is that happening? and how to fix it?

Thx a lot!!


PrinterJob job = PrinterJob.getPrinterJob();

job.setCopies(1);
job.setPrintable(new Printable() {
public int print(Graphics g, PageFormat pf, int pageIndex) {
aSwingComponent.printAll(g);
return Printable.PAGE_EXISTS;
}
});

Hello,

You Printable instance always gives PAGE_EXISTS, for any pageIndex.

It should be:
if (pageIndex==0)
return Printable.PAGE_EXISTS;
else
return PAGA_ ... NOT EXISTS (don't know constant right now,
but easily lookuped up in the doc.

Greetings,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top