/Users/lyon/j4p/src/addBk/print/labels/LabelPrinter.java
|
1 package addBk.print.labels;
2
3 import java.awt.print.PageFormat;
4 import java.awt.print.Paper;
5 import java.awt.print.PrinterException;
6 import java.awt.print.PrinterJob;
7
8 /**
9 * @version 1.0
10 * @author
11 */
12 public class LabelPrinter {
13 private static final int INCH = 72;
14
15 public LabelPrinter(MailingLabelList ll) {
16 PrinterJob pJob = PrinterJob.getPrinterJob();
17 displayPrintDialog(pJob, ll);
18
19 }
20 public static void main(String args[]) {
21 MailingLabelList ll = new MailingLabelList();
22 for (int i=0; i < 20; i++) {
23 MailingLabel ml = new MailingLabel();
24 ml.add("test label#"+i);
25 ml.add("300 North Benson Rd.");
26 ml.add("Fairfield, CT, 06430");
27 ll.add(ml);
28 }
29 new LabelPrinter(ll);
30 System.exit(0);
31 }
32
33
34 private void displayPrintDialog(PrinterJob pJob, MailingLabelList v) {
35 if (pJob.printDialog()) {
36 print(v,pJob);
37 try {
38 print(pJob);
39 } catch (PrinterException e) {
40 System.out.println(e.getMessage());
41 }
42 }
43 }
44
45 private void print(PrinterJob pJob)
46 throws PrinterException {
47 pJob.print();
48 }
49
50 public void print(MailingLabelList ll,PrinterJob pj) {
51 //set up a format
52 PageFormat fmt = pj.defaultPage();
53 //define the orientation
54 fmt.setOrientation(PageFormat.PORTRAIT);
55 //set the paper size
56 Paper paper = fmt.getPaper();
57 paper.setSize(8.5 * INCH, 11 * INCH);
58 //define the full area as imageable
59 paper.setImageableArea(0 * INCH, 0 * INCH, 8.5 * INCH, 11 * INCH);
60 //set this paper as the current format
61 fmt.setPaper(paper);
62
63 //show the page format dialog for the heck of it
64 //pj.pageDialog(fmt);
65 //create a printable page
66 pj.setPrintable(new PrintableLabel(ll), fmt);
67 }
68 }
69