PrinterJob: printDialog() : PrinterJob « java.awt.print « Java by API






PrinterJob: printDialog()

 
import java.awt.Graphics;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;

import javax.swing.JPanel;

public class MainClass extends JPanel {

  public static void main(String[] args) {
    try {
      PrinterJob pjob = PrinterJob.getPrinterJob();
      pjob.setJobName("Graphics Demo Printout");
      pjob.setCopies(1);
      pjob.setPrintable(new Printable() {
        public int print(Graphics pg, PageFormat pf, int pageNum) {
          if (pageNum > 0) // we only print one page
            return Printable.NO_SUCH_PAGE; // ie., end of job

          pg.drawString("www.java2s.com", 10, 10);

          return Printable.PAGE_EXISTS;
        }
      });

      if (pjob.printDialog() == false) // choose printer
        return; 
      pjob.print(); 
    } catch (PrinterException pe) {
      pe.printStackTrace();
    }
  }

}


           
         
  








Related examples in the same category

1.PrinterJob: defaultPage()
2.PrinterJob: getPrinterJob()
3.PrinterJob: pageDialog(PrintRequestAttributeSet attributes)
4.PrinterJob: print()
5.PrinterJob: setCopies(int copies)
6.PrinterJob: setJobName(String jobName)
7.PrinterJob: setPrintable(Printable painter, PageFormat format)