Menu

Java 6 Cast Error

Edward
2006-12-15
2013-05-20
  • Edward

    Edward - 2006-12-15

    Hi there,
    When I try to print a barcode according to the example in:
    http://barbecue.svn.sourceforge.net/viewvc/barbecue/trunk/barbecue/src/zip/examples/PrintingExample.java?view=markup

    I'm get this error:

    Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: net.sourceforge.barbecue.linear.code128.Code128Barcode cannot be cast to java.awt.print.Printable
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    Is this a bug related to Java 6? or to Barbacue? or an error in my code \(which I repeat, is just exactly the same to the code mentioned above. But nevertheless I post it below
    

    try {
                            Barcode b = (Barcode)BarcodeFactory.createCode128(tbl.getValueAt(tbl.convertRowIndexToModel(minSelectionIndex),Attributes.ID.ordinal()).toString());
                            PrinterJob job = PrinterJob.getPrinterJob();
                            job.setPrintable((Printable)b);
                            if (job.printDialog())
                                    job.print();
                    } catch (BarcodeException be) {
                            be.printStackTrace();
                            JOptionPane.showMessageDialog(null,be.getMessage(),
                                    "Error generating Barcode",JOptionPane.ERROR_MESSAGE);
                    } catch (PrinterException pe) {
                            pe.printStackTrace();
                            JOptionPane.showMessageDialog(null,pe.getMessage(),
                                    "Printing Error",JOptionPane.ERROR_MESSAGE);
                    }
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

     
    • chun.park

      chun.park - 2006-12-15

      Why do you try to cast from Barcode to Printable?
      Do you think it's compatible type?
      I do not think so;

       
    • Edward

      Edward - 2006-12-15

      Because of this:

      % javac source.java
      
      source.java:95: cannot find symbol
      symbol  : method setPrintable\(net.sourceforge.barbecue.Barcode\)
      location: class java.awt.print.PrinterJob
                              job.setPrintable\(b\);
                                 ^
      1 error
      
       
    • Buddha Buddy

      Buddha Buddy - 2007-02-07

      Wow, way to be helpful (and wrong), guys.

      The code you are trying to run is from the latest Alpha, and you are probably trying to run it against what was at the time the current release. In the Alpha (which is the ONLY version I can currently download), Barcode IS a Printable, and this example compiles and runs.

      However, it doesn't actually PRINT anything. The results is a completely blank sheet of paper.

       

Log in to post a comment.