Hello all,
I;m trying to do a printjob in java. But there is somthing I do not
understand.
I can;t use the Printable function. I;m using Jbuilding According to
JBuilder this function exists. When I'm trying to compile my project I'll
get the following error:
"Application1.java": cannot resolve symbol: method setPrintable
(printtest.Button1Handler,java.awt.print.PageFormat)in class
java.awt.print.PrinterJob at line 57, column 14
This is my source:
class Button1Handler
implements ActionListener {
public void actionPerformed(ActionEvent e) {
PrinterJob pjob = PrinterJob.getPrinterJob();
PageFormat pf = pjob.defaultPage();
pjob.setPrintable(this,pf);
try {
if (pjob.printDialog()) {
pjob.print();
}
} catch (PrinterException s) {
}
}
}
Does anyone have a solution for this problem?
globe_sa - 12 Dec 2005 14:22 GMT
pjob.setPrintable(this,pf); - 'this' (printtest.Button1Handler) is not
a Printable as spesified by PrinterJob ...
BartCr - 12 Dec 2005 14:32 GMT
I'm assuming this is an innerclass in a gui class (extendsing
java.awt.Component), you need something like this:
class Button1Handler implements ActionListener {
public void actionPerformed(ActionEvent e) {
PrinterJob pjob = PrinterJob.getPrinterJob();
final PageFormat pf = pjob.defaultPage();
pjob.setPrintable(new Printable() {
public int print(Graphics g, PageFormat pageFormat, int
pageIndex) {
if (pageIndex > 0) {
return (NO_SUCH_PAGE);
} else {
Graphics2D g2d = (Graphics2D) g;
g2d.translate(pageFormat.getImageableX(),
pageFormat.getImageableY());
// Turn off double buffering
componentToBePrinted.paint(g2d);
// Turn double buffering back on
return (PAGE_EXISTS);
}
}
}, pf);
try {
if (pjob.printDialog()) {
pjob.print();
}
} catch (PrinterException s) {
}
}
}
But as said, I'm making assumptions here, so no idea if this will solve
your problem.
Bart
Joah Senegal - 12 Dec 2005 15:00 GMT
well,
Thanks for your reply. I'll explain a little bit more what I;m trying to do.
I'm drawing a graphic on a panel. I want to print that graphic.
But I;m going to get crazy in here. nothing works :(:(:(
> Hello all,
>
[quoted text clipped - 28 lines]
>
> Does anyone have a solution for this problem?
Oliver Wong - 12 Dec 2005 21:06 GMT
> Hello all,
>
[quoted text clipped - 28 lines]
>
> Does anyone have a solution for this problem?
The method "setPrintable" expects as its first argument, a Printable,
and, as its second method, a PageFormat. Your second format is a PageFormat
so that's okay, but your first argument is a Button1Handler, not a
Printable. That's what the compiler is complaining about.
You could make Button1Handler implement Printable, but I get the feeling
that you actually meant to pass in something other than "this".
- Oliver