0

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);
        }
    }   
}
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
  • This question is answered here: https://stackoverflow.com/questions/14885993/how-do-i-specify-the-printer-i-want-to-use-in-java –  Mar 22 '19 at 09:49
  • thx for your answer but in that code, you must specify the name of the printer in the programmcode.I want it that the user, who runs the application choose wich printer he wants in printdialog. It is not the same problem.. – user6679516 Mar 25 '19 at 09:23

1 Answers1

0

the problem why java prints on default-printer is:

you have build a jar file and run the application there. Do not try to run on NetBeans-IDE.