Printing a JPanel
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