0

I tried to print a JPanel with this code:

try {
    PrinterJob gap = PrinterJob.getPrinterJob();
    gap.setPrintable(this);
    boolean top = gap.printDialog();

    if(top){
        gap.print();

    }

} catch (PrinterException ex) {
    JOptionPane.showMessageDialog(null, " ERROR DEL PROGRAMA", "ERROR \n " + ex , JOptionPane.INFORMATION_MESSAGE);
}

but, it is appear the menu of configuration for select a printer. I do not want select a printer, I want that it is print from a printer for default or to select the printer in the code.

jh314
  • 27,144
  • 16
  • 62
  • 82

1 Answers1

0

If you don't want to show the print Dialog then remove 'gap.printDialog()', you only need to use 'gap.print()'

You could do something a little like this:

PrinterJob gap = PrinterJob.getPrinterJob();
PageFormat pf = gap.defaultPage();

//Manually assign some of the basic print settings (More advanced things may require you to write your own code)
gap.setJobName("My Print Jnb");

//Set custom page size?
Paper paper = new Paper();
paper.setSize(595, 842);
pf.setPaper(paper);

//Set page orientation?
pf.setOrientation(PageFormat.LANDSCAPE);

//Create the print job with our manual settings
gap.setPrintable(this, pf);

try
{
    //And finally print it out
    gap.print();
}
catch (PrinterException e)
{
    JOptionPane.showMessageDialog(null, " ERROR DEL PROGRAMA", "ERROR \n " + ex , JOptionPane.INFORMATION_MESSAGE);
}

Edit: If you want to choose the printer manually then take a look at this question: How do I specify the printer I want to use in Java?

Community
  • 1
  • 1
sorifiend
  • 5,927
  • 1
  • 28
  • 45