Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4262261

Printing JTable with ImageIcon cells throws IllegalArgumentException

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.2.0
    • client-libs
    • 2d
    • generic
    • generic



      Name: mc57594 Date: 08/12/99


      The application program:
      -----------------------


      import javax.swing.*;
      import javax.swing.table.*;
      import java.awt.print.*;
      import java.util.*;
      import java.awt.*;
      import java.awt.event.*;
      import java.awt.geom.*;
      import java.awt.Dimension;

      public class Report implements Printable
      {
        JFrame frame;
        JTable tableView;

        public Report() {
          frame = new JFrame("Sales Report");
          frame.addWindowListener(new WindowAdapter() {
           public void windowClosing(WindowEvent e) {System.exit(0);}});

          final String[] headers = {"Index", "Icon", "Description", "Open Price",
              "Latest Price", "End Date", "Quantity"};
          final Object[][] data = new Object[200][7];

      ImageIcon image = new ImageIcon("ok.gif");

      int i;
      for (i = 0; i < 200; ++i)
      {
      data[i][0] = new Integer(i);
      data[i][1] = image;
      data[i][2] = new String ("Blue Biro");
      data[i][3] = new String ("0.10");
      data[i][4] = new String ("0.123456789");
      data[i][5] = new Date();
      data[i][6] = new Boolean(true);
      }

          TableModel dataModel = new AbstractTableModel() {
              public int getColumnCount() { return headers.length; }
              public int getRowCount() { return data.length;}
              public Object getValueAt(int row, int col) {
                      return data[row][col];}
              public String getColumnName(int column) {
                      return headers[column];}
              public Class getColumnClass(int col) {
                      return getValueAt(0,col).getClass();}
              public boolean isCellEditable(int row, int col) {
                      return (col==1);}
              public void setValueAt(Object aValue, int row, int column) {
                      data[row][column] = aValue;
             }
           };

           tableView = new JTable(dataModel);
           JScrollPane scrollpane = new JScrollPane(tableView);

           scrollpane.setPreferredSize(new Dimension(500, 80));
           frame.getContentPane().setLayout(new BorderLayout());
           frame.getContentPane().add(BorderLayout.CENTER,scrollpane);
           frame.pack();
           JButton printButton= new JButton();

           printButton.setText("print me!");

           frame.getContentPane().add(BorderLayout.SOUTH,printButton);

           // for faster printing turn double buffering off
           //RepaintManager.currentManager(frame).setDoubleBufferingEnabled(false);

           printButton.addActionListener(
      new ActionListener() {
                public void actionPerformed(ActionEvent evt) {

      new Thread () {
      public void run () {
                      PrinterJob pj=PrinterJob.getPrinterJob();
                      pj.setPrintable(Report.this);
                
                      if (pj.printDialog()) {
      try { pj.print(); } // ??? in the current thread
      catch (Exception printEx) { printEx.printStackTrace (); }
                      }
      }
      }.start();
      }
              });

              frame.setVisible(true);
           }

           public int print(Graphics g,
      PageFormat pageFormat,
                           int pageIndex) throws PrinterException {
              Graphics2D g2 = (Graphics2D) g;
              g2.setColor(Color.black);
              int fontHeight=g2.getFontMetrics().getHeight();
              int fontDescent=g2.getFontMetrics().getDescent();

              //leave room for page number
              double pageHeight = pageFormat.getImageableHeight()-fontHeight;
              double pageWidth = pageFormat.getImageableWidth();
              double tableWidth = (double)
                      tableView.getColumnModel().getTotalColumnWidth();
              double scale = 1;
              if (tableWidth >= pageWidth) {
                      scale = pageWidth / tableWidth;
              }

              double headerHeightOnPage =
      tableView.getTableHeader().getHeight()*scale;
              double tableWidthOnPage=tableWidth*scale;

              double oneRowHeight=(tableView.getRowHeight()+
           tableView.getRowMargin())*scale;
              int numRowsOnAPage =
      (int)((pageHeight-headerHeightOnPage)/oneRowHeight);
              double pageHeightForTable=oneRowHeight*numRowsOnAPage;
              int totalNumPages = (int)Math.ceil((
           (double)tableView.getRowCount())/numRowsOnAPage);
              if(pageIndex>=totalNumPages) {
           return NO_SUCH_PAGE;
              }

              g2.translate(pageFormat.getImageableX(),
                           pageFormat.getImageableY());
      //bottom center
              g2.drawString("Page: "+(pageIndex+1),(int)pageWidth/2-35,
           (int)(pageHeight+fontHeight-fontDescent));

              g2.translate(0f,headerHeightOnPage);
              g2.translate(0f,-pageIndex*pageHeightForTable);

              //If this piece of the table is smaller than the size available,
              //clip to the appropriate bounds.
              if (pageIndex + 1 == totalNumPages) {
                 int lastRowPrinted = numRowsOnAPage * pageIndex;
                 int numRowsLeft = tableView.getRowCount() - lastRowPrinted;
                 g2.setClip(0, (int)(pageHeightForTable * pageIndex),
                   (int) Math.ceil(tableWidthOnPage),
                   (int) Math.ceil(oneRowHeight * numRowsLeft));
              }
              //else clip to the entire area available.
              else{
                   g2.setClip(0, (int)(pageHeightForTable*pageIndex),
                   (int) Math.ceil(tableWidthOnPage),
                   (int) Math.ceil(pageHeightForTable));
              }

            // for faster printing turn double buffering off
            RepaintManager.currentManager(frame).setDoubleBufferingEnabled(false);

              g2.scale(scale,scale);
              tableView.paint(g2);
              g2.scale(1/scale,1/scale);
              g2.translate(0f,pageIndex*pageHeightForTable);
              g2.translate(0f, -headerHeightOnPage);
              g2.setClip(0, 0,(int) Math.ceil(tableWidthOnPage),
                (int)Math.ceil(headerHeightOnPage));
              g2.scale(scale,scale);
              tableView.getTableHeader().paint(g2);//paint header at top

            // return double buffering on
            RepaintManager.currentManager(frame).setDoubleBufferingEnabled(true);

              return Printable.PAGE_EXISTS;
         }

         public static void main(String[] args) {
              new Report();
         }
      }


      The exception:
      -------------
      java.lang.IllegalArgumentException: Zero length string passed to TextLayout constructor.
              at java.lang.Throwable.fillInStackTrace(Native Method)
              at java.lang.Throwable.fillInStackTrace(Compiled Code)
              at java.lang.Throwable.<init>(Compiled Code)
              at java.lang.Exception.<init>(Compiled Code)
              at java.lang.RuntimeException.<init>(RuntimeException.java:47)
              at java.lang.IllegalArgumentException.<init>(IllegalArgumentException.java:43)
              at java.awt.font.TextLayout.<init>(TextLayout.java:337)
              at sun.java2d.PeekGraphics.drawString(PeekGraphics.java:845)
              at javax.swing.plaf.basic.BasicGraphicsUtils.drawString(Compiled Code)
              at javax.swing.plaf.basic.BasicLabelUI.paintEnabledText(Compiled Code)
              at javax.swing.plaf.basic.BasicLabelUI.paint(Compiled Code)
              at javax.swing.plaf.ComponentUI.update(Compiled Code)
              at javax.swing.JComponent.paintComponent(Compiled Code)
              at javax.swing.JComponent.paint(Compiled Code)
              at javax.swing.CellRendererPane.paintComponent(Compiled Code)
              at javax.swing.plaf.basic.BasicTableUI.paintCell(Compiled Code)
              at javax.swing.plaf.basic.BasicTableUI.paintRow(Compiled Code)
              at javax.swing.plaf.basic.BasicTableUI.paint(Compiled Code)
              at javax.swing.plaf.ComponentUI.update(Compiled Code)
              at javax.swing.JComponent.paintComponent(Compiled Code)
              at javax.swing.JComponent.paint(Compiled Code)
              at Report.print(Report.java:183)
              at sun.java2d.RasterPrinterJob.printPage(Compiled Code)
              at sun.java2d.RasterPrinterJob.print(Compiled Code)
              at Report$4.run(Report.java:110)
      (Review ID: 93772)
      ======================================================================

            prr Philip Race
            mchamnessunw Mark Chamness (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: