Java – Hide print dialog

javaprintingswing

I am printing a Jpanel and its working fine, but now I need the print dialog box, which always appear when the print button is clicked. It suppose to go like, when user press print button the default printer should start printing the job with out coming up with a print dialog box. Here is the code, I am using,

Paper paper = new Paper();
paper.setImageableArea(0, 0, 153, 243);
paper.setSize(243, 154);

PageFormat format = new PageFormat();
format.setPaper(paper);
format.setOrientation(PageFormat.LANDSCAPE);

printjob.setPrintable(printable, format);

if (printjob.printDialog() == false)
        return;

try {
        printjob.print();
} catch (PrinterException ex) {
        System.out.println("NO PAGE FOUND." + ex);
}

Thanks.

Best Solution

did you call setPrintService() on printjob?

Or try using printjob.getPrinterJob() to get an instance associated with the default printer.