Foros del Web » Programación para mayores de 30 ;) » Java »

[SOLUCIONADO] implments printable y seleccionar una impresora

Estas en el tema de implments printable y seleccionar una impresora en el foro de Java en Foros del Web. hola gente buenas tardes.. como estan? tengo un metodo en una clase que implemente printable.. y anda perfecto.. siempre y cuando quiera imprimir en la ...
  #1 (permalink)  
Antiguo 22/04/2014, 13:52
 
Fecha de Ingreso: enero-2007
Mensajes: 285
Antigüedad: 17 años, 3 meses
Puntos: 21
implments printable y seleccionar una impresora

hola gente buenas tardes.. como estan?

tengo un metodo en una clase que implemente printable.. y anda perfecto.. siempre y cuando quiera imprimir en la impresora default.. lo que necesito es poder seleccionar la impresora.. por el nombre o como sea.. pego el codigo del metodo..

Código:
 private void btnImprimirActionPerformed(java.awt.event.ActionEvent evt) {                                            
       
      
        try {
            PrinterJob job = PrinterJob.getPrinterJob();
   
            PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
            aset.add(OrientationRequested.LANDSCAPE);
            aset.add(new MediaPrintableArea(2, 2, 29, 90, MediaPrintableArea.MM));
            //aset.add(new PrinterName("Brother QL-570 LE", null));       
     
            job.setPrintable(this);
            job.print(aset);
           
        } catch (PrinterException ex) {
            Logger.getLogger(frmImprimirEtiqueta.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println(ex);
        }
    }
busque bastante.. y como soy relativamente nuevo en java.. no termine de entender bien los cambios que iva encontrando.. y por lo tanto no logre que funcione..

si alguien me tira una soga lo agradeceria bastante.

lo que se imprime es un jpanel...

Última edición por marcusaurelio; 23/04/2014 a las 06:47
  #2 (permalink)  
Antiguo 23/04/2014, 10:25
 
Fecha de Ingreso: enero-2007
Mensajes: 285
Antigüedad: 17 años, 3 meses
Puntos: 21
Respuesta: implments printable y seleccionar una impresora

ya lo resolvi... pero ahora.. necesito hacer un eventlistener para la impresion.. que me valla diciendo en que estado va quedando.. es decir. si comienza, si esta imprimiendo, si termino, y demas.. pero no lo consigo.. para printerJob.. veo que si esta para printJob.. pero no puedo implementarlo..

al menos necesitaria.. saber si la impresora esta prendida, enchufada o activa.. ya que por medio de este code...


Código:
public class MyPrintServiceAttributeListener implements PrintServiceAttributeListener {

    @Override
    public void attributeUpdate(PrintServiceAttributeEvent psae) {
        PrintService service = psae.getPrintService();
        Attribute[] attrs = psae.getAttributes().toArray();
        for (int i = 0; i < attrs.length; i++) {
            String attrName = attrs[i].getName();
            String attrValue = attrs[i].toString();
            System.out.println(service.getName()+": "+attrName+": "+attrValue+"\n");
        }
    }
}
pero solo me dice

Brother QL-570 LE: queued-job-count: 1

Brother QL-570 LE: printer-is-accepting-jobs: accepting-jobs

y siempre me dice printer-is-accepting-jobs: accepting-jobs, por mas que la apague..

hay manera de lograrlo esto.?

dejo el codigo de hasta donde voy.. por si a alguien le sirve
Código:
private void Imprimir() {

        PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
        String printName = "Brother QL-570 LE";
        //String printName="PDFCreator";

        if (services.length > 0) {
            try {
                PrintService myService = null;
                int hayImpresora = 0;
                for (PrintService service : services) {
                    //System.out.println(service.getName());
                    if (service.getName().equals(printName)) {

                        myService = service;
                        hayImpresora = 1;
                        break;
                    }
                }

                if (hayImpresora == 0) {
                    JOptionPane.showMessageDialog(null, "No se Encontro la Impresora: " + printName);
                    //System.exit(0);
                }
                // myService.addPrintServiceAttributeListener(new MyPrintServiceAttributeListener());

                PrinterJob job = PrinterJob.getPrinterJob();

                job.getPrintService().addPrintServiceAttributeListener(null);
                PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
                aset.add(OrientationRequested.LANDSCAPE);
                aset.add(new MediaPrintableArea(2, 2, 29, 90, MediaPrintableArea.MM));

                job.setPrintService(myService);
                job.setPrintable(this);
                job.print(aset);

            } catch (PrinterException ex) {
                Logger.getLogger(dialogImprmirEtiqueta.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

Etiquetas: clase, impresora, metodo
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 18:16.