I have seen every web-site from too may places to mention I have been at this for 3 weeks
System is W7 pro cannon mp560 printer
I have a small app that creates a text file I am trying to read the txt file and print it to the printer. I would rather read the JFormatedTextFields from the app and print these
to the printer but if I can not print a text file the latter is on hold
I have worked with Visual Basic 6 and HTML5 JQuery and can print from these but NOT java
code below is behind a button on a JFrame with Layout(null)
<private void="" mode="hold">
JPanel pBtmR = new JPanel();
pBtmR.setBounds(630, 620, 300, 120);
pBtmR.setLayout(new BoxLayout(pBtmR,BoxLayout.Y_AXIS));
JButton btnPrint = new JButton("PRINT");
//btnPrint.setBounds(650, 650, 70, 30);
//btnPrint.setSize(90, 30);
btnPrint.setBorder(BorderFactory.createMatteBorder(3, 3, 3, 3, Color.blue));
//btnPrint.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
//btnPrint.setBackground(Color.blue);
btnPrint.setMaximumSize(new Dimension(70,30));
btnPrint.setAlignmentX(Component.CENTER_ALIGNMENT);
btnPrint.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
final PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
attributes.add(DialogTypeSelection.NATIVE);
PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.printDialog(attributes);
//String defaultPrinter = PrintServiceLookup.lookupDefaultPrintService().getName();
//System.out.println("Default printer: " + defaultPrinter);
PrintService service = PrintServiceLookup.lookupDefaultPrintService();
FileInputStream in = null;
try {
in = new FileInputStream(new File("C:/Users/Dwight Norris/workspace/ZPzProject/PizzaOrder.txt"));
//System.out.println(in);
} catch (FileNotFoundException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
pras.add(new Copies(1));
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
Doc doc = new SimpleDoc(in, flavor, null);
DocPrintJob job = service.createPrintJob();
//System.out.println(job);
PrintJobWatcher pjw = new PrintJobWatcher(job);
try {
job.print(doc, pras);
} catch (PrintException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
pjw.waitForDone();
try {
in.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// send FF to eject the page
InputStream ff = new ByteArrayInputStream("\f".getBytes());
Doc docff = new SimpleDoc(ff, flavor, null);
DocPrintJob jobff = service.createPrintJob();
pjw = new PrintJobWatcher(jobff);
try {
jobff.print(docff, null);
System.out.println("did I print "+jobff);
} catch (PrintException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
pjw.waitForDone();
}
class PrintJobWatcher {
boolean done = false;
PrintJobWatcher(DocPrintJob job) {
job.addPrintJobListener(new PrintJobAdapter() {
public void printJobCanceled(PrintJobEvent pje) {
allDone();
}
public void printJobCompleted(PrintJobEvent pje) {
allDone();
}
public void printJobFailed(PrintJobEvent pje) {
allDone();
}
public void printJobNoMoreEvents(PrintJobEvent pje) {
allDone();
}
void allDone() {
synchronized (PrintJobWatcher.this) {
done = true;
System.out.println("Printing done ...");
PrintJobWatcher.this.notify();
}
}
});
}
public synchronized void waitForDone() {
try {
while (!done) {
wait();
}
} catch (InterruptedException e) {
}
}
}
});
//END of Print ================================>