Printing a JPanel

J

Justin

I have a class that extends a JPanel and implements Printable. The
class has several JLabel and a JTextArea. I am trying print the JPanel
using the default printer.

I believe the code below is the relevant portion:

public int print(Graphics g, PageFormat pageFormat, int pageIndex)
throws PrinterException {
if (pageIndex >= 1) {
return Printable.NO_SUCH_PAGE;
}

Graphics2D g2D = (Graphics2D) g;
g2D.translate(100, 200);
paintComponents(g2D);
// successful printing of the page
return Printable.PAGE_EXISTS;
}

And the area around the ActionEvent that triggers the print method is:

if (ae.getSource() == btnPrint){

printJob = PrinterJob.getPrinterJob();
printJob.setPrintable(panel);

boolean ok = printJob.printDialog();
if (ok) {
try {
printJob.print();
} catch (Exception pe) {
System.out.println("Printing Exception Occured!");
pe.printStackTrace();
}
}
}


When I press the print button, the print method works, but the document
is blank (I am using MS Office Document Image Writer, so the 'Printer'
is not out of ink).

As always, any help is appreciated, and if I havent supplied enough
code, let me know.

Thanks

Justin
 

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,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top