Click here to Skip to main content
15,510,771 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Java

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 ================================>
Posted
Comments
Richard MacCutchan 27-Feb-15 4:12am    
Please format your code properly: use the code button above the edit box to add <pre> tags around it. you also need to indicate exactly where the problem occurs.

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 J Formatted Text Fields 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

I do not know where the issue is sorry !
Sorry about the code issue and now we have a reply that looks like a solution


Java


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 ================================>
 
Share this answer
 
Comments
Richard MacCutchan 28-Feb-15 5:36am    
Yes, but why did you not just edit your question?
Choroid 28-Feb-15 14:01pm    
Well I looked all over the page for a EDIT button Let me ASK you where it is at
I do not mean to be impolite but the answer I am looking for is why the code
does not work TRUST me I do not post here till I have tried every document know
to man I stated I looked at every site in my POST
Richard MacCutchan 1-Mar-15 3:13am    
If you scroll to the end of your original post you should see a Improve question" which allows you to edit it.

As to why your code does not work, that is not easy to guess without considerably more detail about what results you see against what you expect, or any errors that occur.
Choroid 3-Mar-15 15:41pm    
Your links are to Graphic printing with the old java.awt API
I am looking for help with javax.print API
FYI you have to have the mouse over the page to see the Improve question which
in its self is a long way from the Words EDIT yes I know it has a tool tip
lesson learned Still looking for help with printing to a printer
thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900