Java Printing

J

Joah Senegal

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?
 
G

globe_sa

pjob.setPrintable(this,pf); - 'this' (printtest.Button1Handler) is not
a Printable as spesified by PrinterJob ...
 
B

BartCr

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
 
J

Joah Senegal

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 :(:(:(
 
O

Oliver Wong

Joah Senegal said:
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?

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
 

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

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,241
Latest member
Lisa1997

Latest Threads

Top