I need to check if the printer finished printing whatever it was sent, I found that with the PrintJobListener interface I can do that, but the most important printJobCompleted () method does not work even if the document has been printed, it does not receive any event, only printDataTransferCompleted () works. This is an example
My listener:
public class MyPrintJobListener implements PrintJobListener{
private boolean completed = false;
@Override
public void printDataTransferCompleted(PrintJobEvent pje) {
JOptionPane.showMessageDialog(null, "Transferecia de datos completada");
}
@Override
public void printJobCanceled(PrintJobEvent pje) {
JOptionPane.showMessageDialog(null, "Impresion cancelada");
}
@Override
public void printJobCompleted(PrintJobEvent pje) {
JOptionPane.showMessageDialog(null, "Impresion completada");
}
@Override
public void printJobFailed(PrintJobEvent pje) {
JOptionPane.showMessageDialog(null, "No se pudo imprimir");
}
@Override
public void printJobNoMoreEvents(PrintJobEvent pje) {
// TODO Auto-generated method stub
}
@Override
public void printJobRequiresAttention(PrintJobEvent pje) {
// TODO Auto-generated method stub
}
}
My method:
public void imprimir() throws PrintException{
String s = "ESTO ES UNA PRUEBA";
byte[] bytes = s.getBytes();
PrintService service = PrintServiceLookup.lookupDefaultPrintService();
System.out.println("impresora: " + service);
DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
MyPrintJobListener listener = new MyPrintJobListener();
DocPrintJob pj = service.createPrintJob();
pj.addPrintJobListener(listener);
Doc doc = new SimpleDoc(bytes, flavor, null);
pj.print(doc, null);
}
Edit 1:
The events of the Listener are already working for me! When I have DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE, events do not work for me but it prints (not all special characters).
When I specify another p.e. DocFlavor flavor = DocFlavor.CHAR_ARRAY.TEXT_PLAIN, does not print, but events work.
Can anyone clarify what happens? It's a tm-t20II ticket printer in case you need to know.