I want to print a JPanel which I've created. But if user wants to choose a printer by PrintDialog, no matter which printer is chosen, it's always printing to the default printer in Windows.
Here is my code:
First I create pageBook
public static Book pBook = new Book();
public static class ReportPage implements Printable{
@Override
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) {
if (pageIndex >= 1) {
return Printable.NO_SUCH_PAGE;
}
Graphics2D g2d = (Graphics2D) graphics;
//g2d.scale(0.2275d,0.225d);
pagePanel.paint(g2d);
return Printable.PAGE_EXISTS;
}
}
The print method:
public static void printReport(){
PrinterJob pj = PrinterJob.getPrinterJob();
PageFormat pf = new PageFormat();
Paper paper = pf.getPaper();
pf.setPaper(paper);
pBook.append(new ReportPage(),pf);
pj.setPageable(pBook);
if (pj.printDialog()) {
try {pj.print();}
catch (PrinterException exc){
System.out.println(exc);
}
}
}