Print - JTable with a TITLE on each page

Hi! someone know haw can I print a MY TITLE on each page that I print ?
----->MY TITLE<----
| co1 | col2 | col3 | col4 |
|dtxxz|dtxyz|dtxyz|dtxyz|
|dtxxz|dtxyz|dtxyz|dtxyz|
|dtxxz|dtxyz|dtxyz|dtxyz|
PAGE 1
I try that :
g.drawString("MY TITLE", 100,50);
but the table header hide (writing over) my title
Here is my call :
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 fontDesent = g2.getFontMetrics().getDescent();
     pageFormat.setOrientation(pageFormat.LANDSCAPE);
     //leave room for page number
     double pageHeight = pageFormat.getImageableHeight() - fontHeight;
     double pageWidth = pageFormat.getImageableWidth();
     double tableWidth =
          (double) getCurrentTable().getColumnModel().getTotalColumnWidth();
     double scale = 1;
     if (tableWidth >= pageWidth) {
          scale = pageWidth / tableWidth;
     double headerHeightOnPage =
          getCurrentTable().getTableHeader().getHeight() * scale ;
     double tableWidthOnPage = tableWidth * scale;
     double oneRowHeight =
          (getCurrentTable().getRowHeight() + getCurrentTable().getRowMargin()) * scale;
     int numRowsOnAPage = (int) ((pageHeight - headerHeightOnPage) / oneRowHeight);
     double pageHeightForTable = oneRowHeight * numRowsOnAPage;
     int totalNumPages =
          (int) Math.ceil(((double) getCurrentTable().getRowCount()) / numRowsOnAPage);
     if (pageIndex >= totalNumPages) {
          return NO_SUCH_PAGE;
     g2.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
     g2.drawString(
          "Page: " + (pageIndex + 1),
          (int) pageWidth / 2 - 35,
          (int) (pageHeight + fontHeight - fontDesent));
     //bottom center
     g2.translate(0f, headerHeightOnPage);
     g2.translate(0f, -pageIndex * pageHeightForTable);
     //TODO this next line treats the last page as a full page
     g2.setClip(
          0,
          (int) (pageHeightForTable * pageIndex),
          (int) Math.ceil(tableWidthOnPage),
          (int) Math.ceil(pageHeightForTable));
     g2.scale(scale, scale);
     getCurrentTable().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);
     getCurrentTable().getTableHeader().paint(g2); //paint header at top
     return Printable.PAGE_EXISTS;
Thanks

//Solution-------------------------------------------------------------------------------------------------------
// PRINT : CENTER HEADER TITLE ON EACH PAGE
: PAGE NUMBER ON EACH PAGE
: USE A PREVIEW WINDOW
import javax.swing.*;
import javax.swing.table.*;
import java.awt.print.*;
import java.awt.*;
import java.awt.event.*;
* File: PrintTable.java
* (C) Copyright Corp. 2001 - All Rights Reserved.
* Print a JTable data
public class PrintTable implements Printable {
* Insert the method's description here.
* Creation date: (03-13-2002 13:50:41)
* @param g java.awt.Graphics
* @param pg java.awt.print.PageFormat
* @param i int
public int print(Graphics g, PageFormat pageFormat,
     int pageIndex) throws PrinterException {
     try{
          Graphics2D g2 = (Graphics2D) g;
          g2.setColor(Color.black);
          int fontHeight = g2.getFontMetrics().getHeight();
          int fontDesent = g2.getFontMetrics().getDescent();
          // Normalize the header color
          // he title of the report is not printed if the color of the header is different of these colors
          m_table.getTableHeader().setBackground(new java.awt.Color(204,204,204));
          m_table.getTableHeader().setForeground(new java.awt.Color(0,0,0));
          //leave room for page number
          double pageHeight = pageFormat.getImageableHeight() - fontHeight;
          double pageWidth = pageFormat.getImageableWidth();
          double tableWidth = (double) m_table.getColumnModel().getTotalColumnWidth();
          double scale = 1;
          if (tableWidth >= pageWidth) {
               scale = pageWidth / tableWidth;
          // Get the size of the table header
          double headerHeightOnPage = m_table.getTableHeader().getHeight() * scale;
          // Get the table size on a page
          double tableWidthOnPage = tableWidth * scale;
          // Title Height
          double titleHeightOnPage = 0;
          if( m_title != null ){
               titleHeightOnPage = ((double)m_title.getFontMetrics(m_title.getFont()).getHeight() + 40) * scale;
          // Get size of a row
          double oneRowHeight =
               (m_table.getRowHeight() + m_table.getRowMargin()) * scale;
          int numRowsOnAPage = (int) ((pageHeight - headerHeightOnPage - titleHeightOnPage) / oneRowHeight);
          // Get number of row in a page
          double pageHeightForTable = oneRowHeight * numRowsOnAPage;
          // Get the number of page to print
          int totalNumPages =
               (int) Math.ceil(((double) m_table.getRowCount()) / numRowsOnAPage);
          if (pageIndex >= totalNumPages) {
               return NO_SUCH_PAGE;
          // Print the page label at bottom center
          g2.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
          g2.drawString(
               "Page: " + (pageIndex + 1),
               (int) pageWidth / 2 - 35,
               (int) (pageHeight + fontHeight - fontDesent));
          g2.translate(0f, headerHeightOnPage + titleHeightOnPage);
          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 = m_table.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));
          g2.scale(scale, scale);
          m_table.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);
          // Paint header at the top of the page
          m_table.getTableHeader().paint(g2);
          g2.scale(1 / scale, 1/ scale);
          g2.translate(0f, -titleHeightOnPage);
          g2.setClip(
               0,
               0,
               (int) Math.ceil(tableWidthOnPage),
               (int) Math.ceil(titleHeightOnPage));
          // Paint title
          if( m_strTitle != null ){
               //Center
               g2.translate((pageFormat.getImageableWidth() / 2) -
                              (m_title.getFontMetrics(m_title.getFont()).stringWidth(m_title.getText()) / 2 ),
                              0f );
               g2.drawString(m_title.getText(), 0, 10);
          }else if ( m_title != null ){
               //Center
               g2.translate((pageFormat.getImageableWidth() / 2) -
                              (m_title.getFontMetrics(m_title.getFont()).stringWidth(m_title.getText()) / 2 ),
                              0f );
               m_title.paint(g2);     
     }catch (Exception e){
          e.printStackTrace();
          return Printable.PAGE_EXISTS;
* Show a print dialog box
* Call the method to print
* @param JTable table : table to print
* @param String title : title of the table
public static void print(JTable table, String title) {
     try {
          m_table = table;
          m_strTitle = title;
          m_data     = m_table.getModel();
          PrinterJob prnJob = PrinterJob.getPrinterJob();
          prnJob.setPrintable(PrintTable.instance, prnJob.defaultPage());
          // if the print job is cancelled
          if (!prnJob.printDialog())
               return;
          // print the table
          prnJob.print();
     } catch (PrinterException pe) {
          pe.printStackTrace();
          System.err.println("Printing error: " + pe.toString());
     protected static PrintTable instance     = new PrintTable();
     public static int LANDSCAPE = PageFormat.LANDSCAPE;
     protected static TableModel m_data;
     protected static int m_maxNumPage           = 1;
     protected static String          m_strTitle = null;
     protected static JTable      m_table;
     protected static JLabel      m_title          = null;
     // Paper orientation
     public static int PORTRAIT = PageFormat.PORTRAIT;
     private final static String PREVIEW_LBL = "Aper�u avant impression...";
* Insert the method's description here.
* Creation date: (03-13-2002 10:12:41)
public static void preview(JTable table) {
     m_table = table;
     m_data      = table.getModel();
     PrinterJob prnJob = PrinterJob.getPrinterJob();
     new PrintPreview(PrintTable.instance,
                         PREVIEW_LBL,
                         prnJob.defaultPage());
* Insert the method's description here.
* Creation date: (03-13-2002 10:12:41)
public static void preview(JTable table, PageFormat pf) {
     m_table = table;
     m_data      = table.getModel();
     new PrintPreview(PrintTable.instance,
                         PREVIEW_LBL,
                         pf);
* Insert the method's description here.
* Creation date: (03-13-2002 10:12:41)
public static void preview(JTable table, String title) {
     m_table = table;
     m_data      = table.getModel();
     m_strTitle = title;
     m_title = new JLabel(title);
     PrinterJob prnJob = PrinterJob.getPrinterJob();
     new PrintPreview(PrintTable.instance,
                         PREVIEW_LBL,
                         prnJob.defaultPage());
* Insert the method's description here.
* Creation date: (03-13-2002 10:12:41)
public static void preview(JTable table, String title, PageFormat pf) {
     m_table = table;
     m_data      = table.getModel();
     m_strTitle = title;
     m_title = new JLabel(title);
     new PrintPreview(PrintTable.instance,
                         PREVIEW_LBL,
                         pf);
* Insert the method's description here.
* Creation date: (03-13-2002 10:12:41)
public static void preview(JTable table, JLabel title) {
     m_table = table;
     m_data      = table.getModel();
     m_title = title;
     PrinterJob prnJob = PrinterJob.getPrinterJob();
     new PrintPreview(PrintTable.instance,
                         PREVIEW_LBL,
                         prnJob.defaultPage());
* Insert the method's description here.
* Creation date: (03-13-2002 10:12:41)
public static void preview(JTable table, JLabel title, PageFormat pf) {
     m_table = table;
     m_data      = table.getModel();
     m_title = title;
     new PrintPreview(PrintTable.instance,
                         PREVIEW_LBL,
                         pf);
* Show a print dialog box
* Call the method to print
* @param JTable table : table to print
* @param String title : title of the table
public static void print(JTable table, String title, PageFormat pf) {
     try {
          m_table = table;
          m_strTitle = title;
          m_data     = m_table.getModel();
          PrinterJob prnJob = PrinterJob.getPrinterJob();
          prnJob.setPrintable(PrintTable.instance, pf);
          // if the print job is cancelled
          if (!prnJob.printDialog())
               return;
          // print the table
          prnJob.print();
     } catch (PrinterException pe) {
          pe.printStackTrace();
          System.err.println("Printing error: " + pe.toString());
* Show a print dialog box
* Call the method to print
* @param JTable table : table to print
* @param String title : title of the table
public static void print(JTable table, String title, PageFormat pf, int orientation) {
     try {
          m_table = table;
          m_strTitle = title;
          m_data     = m_table.getModel();
          PrinterJob prnJob = PrinterJob.getPrinterJob();
          prnJob.setPrintable(PrintTable.instance, prnJob.defaultPage());
          pf.setOrientation(orientation);
          // if the print job is cancelled
          if (!prnJob.printDialog())
               return;
          // print the table
          prnJob.print();
     } catch (PrinterException pe) {
          pe.printStackTrace();
          System.err.println("Printing error: " + pe.toString());
* Show a print dialog box
* Call the method to print
* @param JTable table : table to print
* @param String title : title of the table
public static void print(JTable table, JLabel title) {
     try {
          m_table = table;
          m_title = title;
          m_data = table.getModel();
          PrinterJob prnJob = PrinterJob.getPrinterJob();
          prnJob.setPrintable(PrintTable.instance, prnJob.defaultPage());
          // if the print job is cancelled
          if (!prnJob.printDialog())
               return;
          // print the table
          prnJob.print();
     } catch (PrinterException pe) {
          pe.printStackTrace();
          System.err.println("Printing error: " + pe.toString());
* Show a print dialog box
* Call the method to print
* @param JTable table : table to print
* @param String title : title of the table
public static void print(JTable table, JLabel title, PageFormat pf) {
     try {
          m_table = table;
          m_title = title;
          m_data      = table.getModel();
          PrinterJob prnJob = PrinterJob.getPrinterJob();
          prnJob.setPrintable(PrintTable.instance, pf);
          // if the print job is cancelled
          if (!prnJob.printDialog())
               return;
          // print the table
          prnJob.print();
     } catch (PrinterException pe) {
          pe.printStackTrace();
          System.err.println("Printing error: " + pe.toString());
* Show a print dialog box
* Call the method to print
* @param JTable table : table to print
* @param String title : title of the table
public static void print(JTable table, JLabel title, PageFormat pf, int orientation) {
     try {
          m_table = table;
          m_title = title;
          m_data      = table.getModel();
          PrinterJob prnJob = PrinterJob.getPrinterJob();
          prnJob.setPrintable(PrintTable.instance, pf);
          // if the print job is cancelled
          if (!prnJob.printDialog())
               return;
          pf.setOrientation(orientation);
          // print the table
          prnJob.print();
     } catch (PrinterException pe) {
          pe.printStackTrace();
          System.err.println("Printing error: " + pe.toString());
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.util.*;
import java.awt.print.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
public class PrintPreview extends JFrame
protected int m_wPage;
protected int m_hPage;
protected Printable m_target;
protected JComboBox m_cbScale;
protected PreviewContainer m_preview;
private String lblBtnClose           = "Fermer";
private String lblBtnPrint           = "Imprimer...";
protected PageFormat m_pf;
private String[] scales                = { "10 %", "25 %", "50 %", "100 %" };
public PrintPreview(Printable target, String title, PageFormat pf) {
     super(title);
     setSize(600, 400);
     m_target = target;
     m_pf = pf;
     JToolBar tb = new JToolBar();
     JButton bt = new JButton(this.lblBtnPrint);
     ActionListener lst = new ActionListener() {
     public void actionPerformed(ActionEvent e) {
          try {
          // Use default printer, no dialog
          PrinterJob prnJob = PrinterJob.getPrinterJob();
          prnJob.setPrintable(m_target, m_pf);
          setCursor( Cursor.getPredefinedCursor(
               Cursor.WAIT_CURSOR));
          prnJob.print();
          setCursor( Cursor.getPredefinedCursor(
               Cursor.DEFAULT_CURSOR));
          dispose();
          catch (PrinterException ex) {
          ex.printStackTrace();
          System.err.println("Printing error: "+ex.toString());
     bt.addActionListener(lst);
     bt.setAlignmentY(0.5f);
     bt.setMargin(new Insets(4,6,4,6));
     tb.add(bt);
     bt = new JButton(this.lblBtnClose);
     lst = new ActionListener() {
     public void actionPerformed(ActionEvent e) {
          dispose();
     bt.addActionListener(lst);
     bt.setAlignmentY(0.5f);
     bt.setMargin(new Insets(2,6,2,6));
     tb.add(bt);
     m_cbScale = new JComboBox(scales);
     lst = new ActionListener() {
     public void actionPerformed(ActionEvent e) {
          Thread runner = new Thread() {
          public void run() {
               String str = m_cbScale.getSelectedItem().
               toString();
               if (str.endsWith("%"))
               str = str.substring(0, str.length()-1);
               str = str.trim();
               int scale = 0;
               try { scale = Integer.parseInt(str); }
               catch (NumberFormatException ex) { return; }
               int w = (int)(m_wPage*scale/100);
               int h = (int)(m_hPage*scale/100);
               Component[] comps = m_preview.getComponents();
               for (int k=0; k<comps.length; k++) {
               if (!(comps[k] instanceof PagePreview))
                    continue;
               PagePreview pp = (PagePreview)comps[k];
                    pp.setScaledSize(w, h);
               m_preview.doLayout();
               m_preview.getParent().getParent().validate();
          runner.start();
     m_cbScale.addActionListener(lst);
     m_cbScale.setMaximumSize(m_cbScale.getPreferredSize());
     m_cbScale.setEditable(true);
     tb.addSeparator();
     tb.add(m_cbScale);
     getContentPane().add(tb, BorderLayout.NORTH);
     m_preview = new PreviewContainer();
     PrinterJob prnJob = PrinterJob.getPrinterJob();
     PageFormat pageFormat = pf;
     if (pageFormat.getHeight()==0 || pageFormat.getWidth()==0) {
     System.err.println("Unable to determine default page size");
          return;
     m_wPage = (int)(pageFormat.getWidth());
     m_hPage = (int)(pageFormat.getHeight());
     int scale = 10;
     int w = (int)(m_wPage*scale/100);
     int h = (int)(m_hPage*scale/100);
     int pageIndex = 0;
     try {
     while (true) {
          BufferedImage img = new BufferedImage(m_wPage,
          m_hPage, BufferedImage.TYPE_INT_RGB);
          Graphics g = img.getGraphics();
          g.setColor(Color.white);
          g.fillRect(0, 0, m_wPage, m_hPage);
          if (target.print(g, pageFormat, pageIndex) !=
          Printable.PAGE_EXISTS)
          break;
          PagePreview pp = new PagePreview(w, h, img);
          m_preview.add(pp);
          pageIndex++;
     catch (PrinterException e) {
     e.printStackTrace();
     System.err.println("Printing error: "+e.toString());
     JScrollPane ps = new JScrollPane(m_preview);
     getContentPane().add(ps, BorderLayout.CENTER);
     setDefaultCloseOperation(DISPOSE_ON_CLOSE);
     setVisible(true);
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
* Insert the type's description here.
* Creation date: (03-12-2002 14:17:45)
* @author: Alexandre Spain
public class PreviewContainer extends JPanel {
     protected int H_GAP = 16;
     protected int V_GAP = 10;
* PreviewContainer constructor comment.
public PreviewContainer() {
     super();
public void doLayout() {
     Insets ins = getInsets();
     int x = ins.left + H_GAP;
     int y = ins.top + V_GAP;
     int n = getComponentCount();
     if (n == 0)
          return;
     Component comp = getComponent(0);
     Dimension dc = comp.getPreferredSize();
     int w = dc.width;
     int h = dc.height;
     Dimension dp = getParent().getSize();
     int nCol = Math.max((dp.width-H_GAP)/(w+H_GAP), 1);
     int nRow = n/nCol;
     if (nRow*nCol < n)
          nRow++;
     int index = 0;
     for (int k = 0; k<nRow; k++) {
          for (int m = 0; m<nCol; m++) {
               if (index >= n)
                    return;
               comp = getComponent(index++);
               comp.setBounds(x, y, w, h);
               x += w+H_GAP;
          y += h+V_GAP;
          x = ins.left + H_GAP;
public Dimension getMaximumSize() {
     return getPreferredSize();
public Dimension getMinimumSize() {
     return getPreferredSize();
public Dimension getPreferredSize() {
     int n = getComponentCount();
     if (n == 0)
          return new Dimension(H_GAP, V_GAP);
     Component comp = getComponent(0);
     Dimension dc = comp.getPreferredSize();
     int w = dc.width;
     int h = dc.height;
     Dimension dp = getParent().getSize();
     int nCol = Math.max((dp.width-H_GAP)/(w+H_GAP), 1);
     int nRow = n/nCol;
     if (nRow*nCol < n)
          nRow++;
     int ww = nCol*(w+H_GAP) + H_GAP;
     int hh = nRow*(h+V_GAP) + V_GAP;
     Insets ins = getInsets();
     return new Dimension(ww+ins.left+ins.right,
                              hh+ins.top+ins.bottom);
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
* Insert the type's description here.
* Creation date: (03-12-2002 14:17:06)
* @author: Alexandre Spain
public class PagePreview extends JPanel {
     protected int m_w;
     protected int m_h;
     protected Image m_source;
     protected Image m_img;
* PagePreview constructor comment.
* @param target java.awt.print.Printable
public PagePreview(int w, int h, Image source) {
     m_w = w;
     m_h = h;
     m_source= source;
     m_img = m_source.getScaledInstance(m_w, m_h,
          Image.SCALE_SMOOTH);
     m_img.flush();
     setBackground(Color.white);
     setBorder(new MatteBorder(1, 1, 2, 2, Color.black));
public Dimension getMaximumSize() {
     return getPreferredSize();
public Dimension getMinimumSize() {
     return getPreferredSize();
public Dimension getPreferredSize() {
     Insets ins = getInsets();
     return new Dimension(m_w+ins.left+ins.right,
                              m_h+ins.top+ins.bottom);
public void paint(Graphics g) {
     g.setColor(getBackground());
     g.fillRect(0, 0, getWidth(), getHeight());
     g.drawImage(m_img, 0, 0, this);
     paintBorder(g);
public void setScaledSize(int w, int h) {
     m_w = w;
     m_h = h;
     m_img = m_source.getScaledInstance(m_w, m_h,
     Image.SCALE_SMOOTH);
     repaint();

Similar Messages

  • How to Print the Report Column headers on each page of any report

    Can someone offer assistance on how I can go about achieving the ability to show and print the Report Column headers on each page of any report using the Print Attributes of APEX 3.0? These reports will be printed using Excel and Word.
    I read a thread with a similar request but the answer seems vague for my level of understanding.
    I am connected to a print server and using BI Publisher.
    Thanks,
    Ric

    Hello Ric,
    >> These reports will be printed using Excel and Word.
    I'm not sure I understand what you are trying to do. You are using a very powerful tool – BI Publisher – why do you need Excel or Word for printing? Is there a special need only Excel or Word can provide?
    One of the major advantages of using BI Publisher is that it's taking care of all these tedious tasks like reasonable page breaking, headers and footers, and also table headers.
    Regards,
    Arie.

  • Print Jtable with multiline header?

    i want to print jtable with multi-lines header and footer
    using the print function that takes MessageFormat as header and footer
    i used MessageFormat header = new MessageFormat("hello\r\nworld");and i used '\n' only
    but it was printed all in the same line
    thnx in advance

    You can try something on the below lines. Set your custom renderer for the tableheader.
    public class MultiLineHeaderRenderer extends DefaultTableCellRenderer{
            public Component getTableCellRendererComponent(JTable table, Object value,
                             boolean isSelected, boolean hasFocus, int row, int column) {
                         JLabel label = (JLabel) super.getTableCellRendererComponent( table,  value,
                              isSelected,  hasFocus,  row,  column);
                        label.setText("<html>a<br>b</html>");
                        return label;
        }Not the best way to do it. And might need to some modifications too to set the font position etc.

  • Print JTable with Multiple pages and rows

    I took the printing example at http://java.sun.com/developer/onlineTraining/Programming/JDCBook/advprint.html#pe and modified it a bit to include the following:
    1) To Print Multiple pages
    2) To wrap lines that is too long for the column
    3) To print with a more proffesional style, so that it doesn't look like a screen capture is printed
    4) To align the numbers to the right and center column headings
    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;
    import javax.print.*;
    import javax.print.attribute.*;
    import javax.print.attribute.standard.*;
    import java.text.*;
    public class Report implements Printable
         private final int LEFT_ALIGN = -1;
         private final int CENTER_ALIGN = 0;
         private final int RIGHT_ALIGN = 1;
         private JFrame frame;
         private JTable tableView;
         private String lastPrintDate;
         private Font defaultFont;
         private Font headerFont;
         private Font footerFont;
         private int headerHeight;
         private int footerHeight;
         private int cellBuffer = 5;
         private boolean first_pass;
         private ArrayList pages;
         public Report()
              frame = new JFrame("Sales Report");
              frame.addWindowListener(new WindowAdapter()
                   public void windowClosing(WindowEvent e)
                        System.exit(0);
              final String[] headers =
                   "ID",
                   "Description",
                   "open price",
                   "latest price",
                   "End Date",
                   "Quantity"
              int count = 0;
              final Object[][] data =
                   {new Integer(count++), "Box of BirosBox of BirosBox of BirosBox of BirosBox of BirosBox of BirosBox of BirosBox of BirosBox of BirosBox of BirosBox of BirosBox of BirosBox of BirosBox of Biros ppppppppppppppp", "1.00", "4.99", new Date(), new Integer(200000)},
                   {new Integer(count++), "Blue Biro", "0.10", "0.14", new Date(), new Integer(1)},
                   {new Integer(count++), "legal pad", "1.00", "2.49", new Date(), new Integer(1)},
                   {new Integer(count++), "tape", "1.00", "1.49", new Date(), new Integer(1)},
                   {new Integer(count++), "stapler", "4.00", "4.49", new Date(), new Integer(1)},
                   {new Integer(count++), "Box of Biros", "1.00", "4.99", new Date(), new Integer(2)},
                   {new Integer(count++), "Blue Biro", "0.10", "0.14", new Date(), new Integer(1)},
                   {new Integer(count++), "legal pad", "1.00", "2.49", new Date(), new Integer(1)},
                   {new Integer(count++), "tape", "1.00", "1.49", new Date(), new Integer(1)},
                   {new Integer(count++), "stapler", "4.00", "4.49", new Date(), new Integer(1)},
                   {new Integer(count++), "Box of Biros", "1.00", "4.99", new Date(), new Integer(2)},
                   {new Integer(count++), "Blue Biro", "0.10", "0.14", new Date(), new Integer(1)},
                   {new Integer(count++), "legal pad", "1.00", "2.49", new Date(), new Integer(1)},
                   {new Integer(count++), "tape", "1.00", "1.49", new Date(), new Integer(1)},
                   {new Integer(count++), "stapler", "4.00", "4.49", new Date(), new Integer(1)},
                   {new Integer(count++), "Box of Biros", "1.00", "4.99", new Date(), new Integer(2)},
                   {new Integer(count++), "Blue Biro", "0.10", "0.14", new Date(), new Integer(1)},
                   {new Integer(count++), "legal pad", "1.00", "2.49", new Date(), new Integer(1)},
                   {new Integer(count++), "tape", "1.00", "1.49", new Date(), new Integer(1)},
                   {new Integer(count++), "stapler", "4.00", "4.49", new Date(), new Integer(1)},
                   {new Integer(count++),  "Box of Biros", "1.00", "4.99", new Date(), new Integer(2)},
                   {new Integer(count++),  "Blue Biro", "0.10", "0.14", new Date(), new Integer(1)},
                   {new Integer(count++),  "legal pad", "1.00", "2.49", new Date(), new Integer(1)},
                   {new Integer(count++),  "tape", "1.00", "1.49", new Date(), new Integer(1)},
                   {new Integer(count++),  "stapler", "4.00", "4.49", new Date(), new Integer(1)},
                   {new Integer(count++),  "Box of Biros", "1.00", "4.99", new Date(), new Integer(2)},
                   {new Integer(count++),  "Blue Biro", "0.10", "0.14", new Date(), new Integer(1)},
                   {new Integer(count++),  "legal pad", "1.00", "2.49", new Date(), new Integer(1)},
                   {new Integer(count++),  "tape", "1.00", "1.49", new Date(), new Integer(1)},
                   {new Integer(count++),  "stapler", "4.00", "4.49", new Date(), new Integer(1)},
                   {new Integer(count++),  "Box of Biros", "1.00", "4.99", new Date(), new Integer(2)}
              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)
                        doPrint();
              frame.setVisible(true);
          * Reset variables before printing
         private void prepareForPrint()
              pages = new ArrayList();
              first_pass = true;
          * Display a print dialog with some hardcoded defaults
          * The print fonts are also hardcoded
         public void doPrint()
              try
                   String jobName = "Java Report";
                   defaultFont = new Font("Arial", Font.PLAIN, 8);
                   footerFont = new Font("Arial", Font.PLAIN, 6);
                   headerFont = new Font("Arial", Font.BOLD, 10);
                   PrinterJob prnJob = PrinterJob.getPrinterJob();
                   prnJob.setPrintable(this);
                   PrintRequestAttributeSet prnSet = new HashPrintRequestAttributeSet();
                   prnSet.add(new Copies(1));
                   prnSet.add(new JobName(jobName, null));
                   prnSet.add(MediaSizeName.ISO_A4);
                   PageFormat pf = prnJob.defaultPage();
                   pf.setOrientation(java.awt.print.PageFormat.PORTRAIT);
                   prnJob.setJobName(jobName);
                   PrintService[] services = PrinterJob.lookupPrintServices();
                   if (services.length > 0)
                        if (prnJob.printDialog(prnSet))
                              * Get print date
                             String dateFormat = "dd/MM/yyyy HH:mm:ss";
                             DateFormat m_DateFormat = new SimpleDateFormat(dateFormat);
                             lastPrintDate = m_DateFormat.format(new Date()).toString();
                             prepareForPrint();
                             prnJob.print(prnSet);
                   else
                        JOptionPane.showMessageDialog(frame, "No Printer was found!!", "Printer Error", JOptionPane.ERROR_MESSAGE);
                        return;
              catch (PrinterException e)
                   e.printStackTrace();
         public int print(Graphics g, PageFormat pageFormat, int pageIndex) throws PrinterException
               * Check if this is the first time the print method is called for this print action.
               * It is not guaranteed that the print will be called with synchronous pageIndex'es,
               * so we need to calculate the number of pages and which rows appear on which pages.
               * Then the correct page will be printed regardless of which pageIndex is sent through.
              if (first_pass)
                   calcPages(g, pageFormat);
              first_pass = false;
              // Stop printing if the pageIndex is out of range
              if (pageIndex >= pages.size())
                   return NO_SUCH_PAGE;
              Graphics2D     g2 = (Graphics2D) g;
              g2.setColor(Color.black);
              // The footer will be one line at the bottom of the page, cater for this.
              g2.setFont(footerFont);
              footerHeight = g2.getFontMetrics().getHeight() + g2.getFontMetrics().getDescent();
              g2.setFont(defaultFont);
              FontMetrics fontMetrics = g2.getFontMetrics();
              int fontHeight = fontMetrics.getHeight();
              int fontDescent = fontMetrics.getDescent();
              double pageHeight = pageFormat.getImageableHeight() + pageFormat.getImageableY();
              double pageWidth = pageFormat.getImageableWidth();
              double tableWidth = (double) tableView.getColumnModel().getTotalColumnWidth();
              // Shrink or expand the table to fit the page width
              double scale = pageWidth / (tableWidth+ (cellBuffer * tableView.getColumnCount()));
              // Calculate the width in pixels for each column
              double[] columnWidths = new double[tableView.getColumnCount()];
              for(int i = 0; i < tableView.getColumnCount(); i++)
                   columnWidths[i] = (double)tableView.getColumnModel().getColumn(i).getWidth() * scale;
              // Reset the view to the start of the page
              g2.translate(0, 0);
              // Draw a rectangle to see the printable area
              g2.draw3DRect((int)pageFormat.getImageableX(),
                        (int)pageFormat.getImageableY(),
                        (int)pageFormat.getImageableWidth(),
                        (int)pageFormat.getImageableHeight(),
                        false);
              // Calculate the header height
              g2.setFont(headerFont);
              fontMetrics = g2.getFontMetrics();
              // Print the headers and retreive the starting position for the data
              int next_row = printLine(g2, pageFormat, fontMetrics, -1, (int)pageFormat.getImageableY() + fontHeight, columnWidths);
              g2.setFont(defaultFont);
              fontMetrics = g2.getFontMetrics();
              // Start printing the detail
              ArrayList page = (ArrayList)pages.get(pageIndex);
              int start = ((Integer)page.get(0)).intValue();
              int end = ((Integer)page.get(1)).intValue();
              for (int i = start; i <= end; i++)
                   next_row = printLine(g2, pageFormat, fontMetrics, i, next_row, columnWidths);
              // Print the footer
              g2.setFont(footerFont);
              String pageFooter = "Page " + (pageIndex + 1) + " - " + lastPrintDate;
              g2.drawString(pageFooter,
                             (int)pageFormat.getWidth() / 2 - (fontMetrics.stringWidth(pageFooter) / 2),
                             (int)(pageHeight - fontDescent));
              return PAGE_EXISTS;
          * We can't guarantee that the same amount of rows will be displayed on each page,
          * the row heights are dynamic and may wrap onto 2 or more lines.
          * Thus we need to calculate the height of each row and then test how may rows
          * fit on a specific page. eg. Page 1 contains rows 1 to 10, Page 2 contains rows 11 to 15 etc.
         public void calcPages(Graphics g, PageFormat pageFormat) throws PrinterException
              Graphics2D     g2 = (Graphics2D) g;
              g2.setColor(Color.black);
              // The footer will be one line at the bottom of the page, cater for this.
              g2.setFont(footerFont);
              footerHeight = g2.getFontMetrics().getHeight() + g2.getFontMetrics().getDescent();
              g2.setFont(defaultFont);
              FontMetrics fontMetrics = g2.getFontMetrics();
              int fontHeight = fontMetrics.getHeight();
              int fontDescent = fontMetrics.getDescent();
              double pageHeight = pageFormat.getImageableHeight() - fontHeight;
              double pageWidth = pageFormat.getImageableWidth();
              double tableWidth = (double) tableView.getColumnModel().getTotalColumnWidth();
              // Shrink or expand the table to fit the page width
              double scale = pageWidth / (tableWidth+ (cellBuffer * tableView.getColumnCount()));
              // Calculate the width in pixels for each column
              double[] columnWidths = new double[tableView.getColumnCount()];
              for(int i = 0; i < tableView.getColumnCount(); i++)
                   columnWidths[i] = (double)tableView.getColumnModel().getColumn(i).getWidth() * scale;
              // Calculate the header height
              int maxHeight = 0;
              g2.setFont(headerFont);
              fontMetrics = g2.getFontMetrics();
              for (int j = 0; j < tableView.getColumnCount(); j++)
                   String value = tableView.getColumnName(j).toString();
                   int numLines = (int)Math.ceil(fontMetrics.stringWidth(value) / columnWidths[j]);
                   if (numLines > maxHeight)
                        maxHeight = numLines;
              headerHeight = g2.getFontMetrics().getHeight() * maxHeight;
              g2.setFont(defaultFont);
              fontMetrics = g2.getFontMetrics();
              int pageNum = 0;
              int bottom_of_page = (int)(pageFormat.getImageableHeight() + pageFormat.getImageableY()) - footerHeight;
              int prev_row = 0;
              int next_row = (int)pageFormat.getImageableY() + fontHeight + headerHeight;
              int i = 0;
              ArrayList page = new ArrayList();
              page.add(new Integer(0));
              for (i = 0; i < tableView.getRowCount(); i++)
                   maxHeight = 0;
                   for (int j = 0; j < tableView.getColumnCount(); j++)
                        String value = tableView.getValueAt(i, j).toString();
                        int numLines = (int)Math.ceil(fontMetrics.stringWidth(value) / columnWidths[j]);
                        if (numLines > maxHeight)
                             maxHeight = numLines;
                   prev_row = next_row;
                   next_row += (fontHeight * maxHeight);
                   // If we've reached the bottom of the page then set the current page's end row
                   if (next_row > bottom_of_page)
                        page.add(new Integer(i - 1));
                        pages.add(page);
                        page = new ArrayList();
                        page.add(new Integer(i));
                        pageNum++;
                        next_row = (int)pageFormat.getImageableY()
                                       + fontHeight
                                       + ((int)pageFormat.getHeight() * pageNum)
                                       + headerHeight;
                        bottom_of_page = (int)(pageFormat.getImageableHeight()
                                            + pageFormat.getImageableY())
                                            + ((int)pageFormat.getHeight() * pageNum)
                                            - footerHeight;
                        //Include the current row on the next page, because there is no space on this page
                        i--;
              page.add(new Integer(i - 1));
              pages.add(page);
          * Print the headers or a row from the table to the graphics context
          * Return the position of the row following this one
         public int printLine(Graphics2D g2,
                                       PageFormat pageFormat,
                                       FontMetrics fontMetrics,
                                       int rowNum,
                                       int next_row,
                                       double[] columnWidths)
                   throws PrinterException
              int lead = 0;
              int maxHeight = 0;
              for (int j = 0; j < tableView.getColumnCount(); j++)
                   String value = null;
                   int align = LEFT_ALIGN;
                   if (rowNum > -1)
                        Object obj = tableView.getValueAt(rowNum, j);
                        if (obj instanceof Number)
                             align = RIGHT_ALIGN;
                        value = obj.toString();
                   else
                        align = CENTER_ALIGN;
                        value = tableView.getColumnName(j);
                   int numLines = (int)Math.ceil(fontMetrics.stringWidth(value) / columnWidths[j]);
                   if (numLines > maxHeight)
                        maxHeight = numLines;
                   if (fontMetrics.stringWidth(value) < columnWidths[j])
                        // Single line
                        int offset = 0;
                        // Work out the offset from the start of the column to display alignment correctly
                        switch (align)
                             case RIGHT_ALIGN: offset = (int)(columnWidths[j] - fontMetrics.stringWidth(value)); break;
                             case CENTER_ALIGN: offset = (int)(columnWidths[j] - fontMetrics.stringWidth(value)) / 2; break;
                             default: offset = 0; break;
                        g2.drawString(value,
                                       lead + (int)(pageFormat.getImageableX() + offset),
                                       next_row);
                   else
                        for(int a = 0; a < numLines; a++)
                             //Multi-Line
                             int x = 0;
                             int width = 0;
                             for(x = 0; x < value.length(); x++)
                                  width += fontMetrics.charWidth(value.charAt(x));
                                  if (width > columnWidths[j])
                                       break;
                             int offset = 0;
                             // Work out the offset from the start of the column to display alignment correctly
                             switch (align)
                                  case RIGHT_ALIGN: offset = (int)(columnWidths[j] - fontMetrics.stringWidth(value)); break;
                                  case CENTER_ALIGN: offset = (int)(columnWidths[j] - fontMetrics.stringWidth(value)) / 2; break;
                                  default: offset = 0; break;
                             g2.drawString(value.substring(0, x),
                                            lead + (int)(pageFormat.getImageableX() + offset),
                                            next_row + (fontMetrics.getHeight() * a));                    
                             value = value.substring(x);
                   lead += columnWidths[j] + cellBuffer;
              // Draw a solid line below the row
              g2.draw(new Line2D.Double(pageFormat.getImageableX(),
                             next_row + (fontMetrics.getHeight() * (maxHeight - 1)) + fontMetrics.getDescent(),
                             pageFormat.getImageableY() + pageFormat.getImageableWidth(),
                             next_row + (fontMetrics.getHeight() * (maxHeight - 1)) + fontMetrics.getDescent()));
              // Return the position of the row following this one
              return next_row + (fontMetrics.getHeight() * maxHeight);
         public static void main(String[] args)
              new Report();
    }

    Fixed some bugs and added a title. Just pass in a JTable and the class will do the rest.
    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 javax.print.*;
    import javax.print.attribute.*;
    import javax.print.attribute.standard.*;
    import java.text.*;
    import java.math.*;
    public class PrintJTable implements Printable
         private final int LEFT_ALIGN = -1;
         private final int CENTER_ALIGN = 0;
         private final int RIGHT_ALIGN = 1;
         private JFrame m_parent;
         private String m_title;
         private JTable tableView;
         private String lastPrintDate;
         private Font defaultFont;
         private Font headerFont;
         private Font footerFont;
         private int headerHeight;
         private int footerHeight;
         private int cellBuffer = 5;
         private boolean first_pass;
         private ArrayList pages;
         public PrintJTable(JFrame parent, JTable table)
              m_parent = parent;
              tableView = table;
              doPrint();
         public PrintJTable(JFrame parent, String title, JTable table)
              m_parent = parent;
              m_title = title;
              tableView = table;
              doPrint();
          * Reset variables before printing
         private void prepareForPrint()
              pages = new ArrayList();
              first_pass = true;
          * Display a print dialog with some hardcoded defaults
          * The print fonts are also hardcoded
         public void doPrint()
              try
                   String jobName = "Java Report";
                   defaultFont = new Font("Arial", Font.PLAIN, 8);
                   footerFont = new Font("Arial", Font.PLAIN, 6);
                   headerFont = new Font("Arial", Font.BOLD, 8);
                   PrinterJob prnJob = PrinterJob.getPrinterJob();
                   prnJob.setPrintable(this);
                   PrintRequestAttributeSet prnSet = new HashPrintRequestAttributeSet();
                   prnSet.add(new Copies(1));
                   prnSet.add(new JobName(jobName, null));
                   prnSet.add(MediaSizeName.ISO_A4);
                   PageFormat pf = prnJob.defaultPage();
                   pf.setOrientation(java.awt.print.PageFormat.PORTRAIT);
                   prnJob.setJobName(jobName);
                   PrintService[] services = PrinterJob.lookupPrintServices();
                   if (services.length > 0)
                        if (prnJob.printDialog(prnSet))
                              * Get print date
                             String dateFormat = "dd/MM/yyyy HH:mm:ss";
                             DateFormat m_DateFormat = new SimpleDateFormat(dateFormat);
                             lastPrintDate = m_DateFormat.format(new Date()).toString();
                             prepareForPrint();
                             prnJob.print(prnSet);
                   else
                        JOptionPane.showMessageDialog(m_parent, "No Printer was found!!", "Printer Error", JOptionPane.ERROR_MESSAGE);
                        return;
              catch (PrinterException e)
                   e.printStackTrace();
         public int print(Graphics g, PageFormat pageFormat, int pageIndex) throws PrinterException
               * Check if this is the first time the print method is called for this print action.
               * It is not guaranteed that the print will be called with synchronous pageIndex'es,
               * so we need to calculate the number of pages and which rows appear on which pages.
               * Then the correct page will be printed regardless of which pageIndex is sent through.
              if (first_pass)
                   calcPages(g, pageFormat);
              first_pass = false;
              // Stop printing if the pageIndex is out of range
              if (pageIndex >= pages.size())
                   return NO_SUCH_PAGE;
              Graphics2D     g2 = (Graphics2D) g;
              g2.setColor(Color.black);
              // The footer will be one line at the bottom of the page, cater for this.
              g2.setFont(footerFont);
              footerHeight = g2.getFontMetrics().getHeight() + g2.getFontMetrics().getDescent();
              g2.setFont(defaultFont);
              FontMetrics fontMetrics = g2.getFontMetrics();
              int fontHeight = fontMetrics.getHeight();
              int fontDescent = fontMetrics.getDescent();
              double pageHeight = pageFormat.getImageableHeight() + pageFormat.getImageableY();
              double pageWidth = pageFormat.getImageableWidth();
              double tableWidth = (double) tableView.getColumnModel().getTotalColumnWidth();
              // Shrink or expand the table to fit the page width
              double scale = (pageWidth - (cellBuffer * tableView.getColumnCount())) / tableWidth;
              // Calculate the width in pixels for each column
              double[] columnWidths = new double[tableView.getColumnCount()];
              double test = 0;
              for(int i = 0; i < tableView.getColumnCount(); i++)
                   columnWidths[i] = (double)Math.floor(tableView.getColumnModel().getColumn(i).getWidth() * scale);
                   test += columnWidths;
              // Reset the view to the start of the page
              g2.translate(0, 0);
              // Draw a rectangle to see the printable area
              g2.draw3DRect((int)pageFormat.getImageableX(),
                        (int)pageFormat.getImageableY(),
                        (int)pageFormat.getImageableWidth(),
                        (int)pageFormat.getImageableHeight(),
                        false);
              // Calculate the header height
              g2.setFont(headerFont);
              fontMetrics = g2.getFontMetrics();
              // Print the headers and retreive the starting position for the data
              int next_row = (int)pageFormat.getImageableY() + fontMetrics.getHeight();
              if ((m_title != null) && (!m_title.equalsIgnoreCase("")))
                   g2.drawString(m_title,
                                       (int)(pageFormat.getImageableX()),
                                       next_row);
                   Color current_color = g2.getColor();
                   g2.setColor(Color.lightGray);
                   int y = next_row + fontMetrics.getDescent();
                   g2.draw(new Line2D.Double(pageFormat.getImageableX(),
                                  y,
                                  (pageFormat.getImageableY() + pageFormat.getImageableWidth()),
                                  y));
                   g2.setColor(current_color);
                   next_row += fontMetrics.getHeight();
              next_row = printLine(g2, pageFormat, fontMetrics, -1, next_row, columnWidths);
              g2.setFont(defaultFont);
              fontMetrics = g2.getFontMetrics();
              // Start printing the detail
              ArrayList page = (ArrayList)pages.get(pageIndex);
              int start = ((Integer)page.get(0)).intValue();
              int end = ((Integer)page.get(1)).intValue();
              for (int i = start; i <= end; i++)
                   next_row = printLine(g2, pageFormat, fontMetrics, i, next_row, columnWidths);
              // Print the footer
              g2.setFont(footerFont);
              String pageFooter = "Page " + (pageIndex + 1) + " - " + lastPrintDate;
              g2.drawString(pageFooter,
                             (int)pageFormat.getWidth() / 2 - (fontMetrics.stringWidth(pageFooter) / 2),
                             (int)(pageHeight - fontDescent));
              return PAGE_EXISTS;
         * We can't guarantee that the same amount of rows will be displayed on each page,
         * the row heights are dynamic and may wrap onto 2 or more lines.
         * Thus we need to calculate the height of each row and then test how may rows
         * fit on a specific page. eg. Page 1 contains rows 1 to 10, Page 2 contains rows 11 to 15 etc.
         public void calcPages(Graphics g, PageFormat pageFormat) throws PrinterException
              Graphics2D     g2 = (Graphics2D) g;
              g2.setColor(Color.black);
              // The footer will be one line at the bottom of the page, cater for this.
              g2.setFont(footerFont);
              footerHeight = g2.getFontMetrics().getHeight() + g2.getFontMetrics().getDescent();
              g2.setFont(defaultFont);
              FontMetrics fontMetrics = g2.getFontMetrics();
              int fontHeight = fontMetrics.getHeight();
              int fontDescent = fontMetrics.getDescent();
              double pageHeight = pageFormat.getImageableHeight() - fontHeight;
              double pageWidth = pageFormat.getImageableWidth();
              double tableWidth = (double) tableView.getColumnModel().getTotalColumnWidth();
              // Shrink or expand the table to fit the page width
              double scale = (pageWidth - (cellBuffer * tableView.getColumnCount())) / tableWidth;
              // Calculate the width in pixels for each column
              double[] columnWidths = new double[tableView.getColumnCount()];
              for(int i = 0; i < tableView.getColumnCount(); i++)
                   columnWidths[i] = (double)Math.floor(tableView.getColumnModel().getColumn(i).getWidth() * scale);
              // Calculate the header height
              int maxHeight = 0;
              g2.setFont(headerFont);
              fontMetrics = g2.getFontMetrics();
              headerHeight = 0;
              if ((m_title != null) && (!m_title.equalsIgnoreCase("")))
                   headerHeight = fontMetrics.getHeight();
              for (int j = 0; j < tableView.getColumnCount(); j++)
                   String value = tableView.getColumnName(j).toString();
                   int numLines = (int)Math.ceil(fontMetrics.stringWidth(value) / columnWidths[j]);
                   if (numLines > maxHeight)
                        maxHeight = numLines;
              headerHeight += g2.getFontMetrics().getHeight() * maxHeight;
              g2.setFont(defaultFont);
              fontMetrics = g2.getFontMetrics();
              int pageNum = 0;
              int bottom_of_page = (int)(pageFormat.getImageableHeight() + pageFormat.getImageableY()) - footerHeight;
              int prev_row = 0;
              int next_row = (int)pageFormat.getImageableY() + fontHeight + headerHeight;
              int i = 0;
              ArrayList page = new ArrayList();
              page.add(new Integer(0));
              for (i = 0; i < tableView.getRowCount(); i++)
                   maxHeight = 0;
                   for (int j = 0; j < tableView.getColumnCount(); j++)
                        String value = formatObject(tableView.getValueAt(i, j));
                        int numLines = (int)Math.ceil(fontMetrics.stringWidth(value) / columnWidths[j]);
                        if (numLines > maxHeight)
                             maxHeight = numLines;
                   prev_row = next_row;
                   next_row += (fontHeight * maxHeight);
                   // If we've reached the bottom of the page then set the current page's end row
                   if (next_row > bottom_of_page)
                        page.add(new Integer(i - 1));
                        pages.add(page);
                        page = new ArrayList();
                        page.add(new Integer(i));
                        pageNum++;
                        next_row = (int)pageFormat.getImageableY()
                                       + fontHeight
                                       + ((int)pageFormat.getHeight() * pageNum)
                                       + headerHeight;
                        bottom_of_page = (int)(pageFormat.getImageableHeight()
                                            + pageFormat.getImageableY())
                                            + ((int)pageFormat.getHeight() * pageNum)
                                            - footerHeight;
                        //Include the current row on the next page, because there is no space on this page
                        i--;
              page.add(new Integer(i - 1));
              pages.add(page);
         * Print the headers or a row from the table to the graphics context
         * Return the position of the row following this one
         public int printLine(Graphics2D g2,
                                       PageFormat pageFormat,
                                       FontMetrics fontMetrics,
                                       int rowNum,
                                       int next_row,
                                       double[] columnWidths)
                   throws PrinterException
              int lead = 0;
              int maxHeight = 0;
              for (int j = 0; j < tableView.getColumnCount(); j++)
                   String value = null;
                   int align = LEFT_ALIGN;
                   if (rowNum > -1)
                        Object obj = tableView.getValueAt(rowNum, j);
                        if (obj instanceof Number)
                             align = RIGHT_ALIGN;
                        value = formatObject(obj);
                   else
                        //align = CENTER_ALIGN;
                        value = tableView.getColumnName(j);
                   int numLines = (int)Math.ceil(fontMetrics.stringWidth(value) / columnWidths[j]);
                   if (numLines > maxHeight)
                        maxHeight = numLines;
                   if (fontMetrics.stringWidth(value) < columnWidths[j])
                        // Single line
                        int offset = 0;
                        // Work out the offset from the start of the column to display alignment correctly
                        switch (align)
                             case RIGHT_ALIGN: offset = (int)(columnWidths[j] - fontMetrics.stringWidth(value)); break;
                             case CENTER_ALIGN: offset = (int)(columnWidths[j] - fontMetrics.stringWidth(value)) / 2; break;
                             default: offset = 0; break;
                        g2.drawString(value,
                                       lead + (int)(pageFormat.getImageableX() + offset),
                                       next_row);
                   else
                        for(int a = 0; a < numLines; a++)
                             //Multi-Line
                             int x = 0;
                             int width = 0;
                             for(x = 0; x < value.length(); x++)
                                  width += fontMetrics.charWidth(value.charAt(x));
                                  if (width > columnWidths[j])
                                       break;
                             int offset = 0;
                             // Work out the offset from the start of the column to display alignment correctly
                             switch (align)
                                  case RIGHT_ALIGN: offset = (int)(columnWidths[j] - fontMetrics.stringWidth(value.substring(0, x))); break;
                                  case CENTER_ALIGN: offset = (int)(columnWidths[j] - fontMetrics.stringWidth(value.substring(0, x))) / 2; break;
                                  default: offset = 0; break;
                             g2.drawString(value.substring(0, x),
                                            lead + (int)(pageFormat.getImageableX() + offset),
                                            next_row + (fontMetrics.getHeight() * a));                    
                             value = value.substring(x);
                   lead += columnWidths[j] + cellBuffer;
              // Draw a solid line below the row
              Color current_color = g2.getColor();
              g2.setColor(Color.lightGray);
              int y = next_row + (fontMetrics.getHeight() * (maxHeight - 1)) + fontMetrics.getDescent();
              g2.draw(new Line2D.Double(pageFormat.getImageableX(),
                             y,
                             (pageFormat.getImageableY() + pageFormat.getImageableWidth()),
                             y));
              g2.setColor(current_color);
              // Return the position of the row following this one
              return next_row + (fontMetrics.getHeight() * maxHeight);
         public String formatObject(Object obj)
              String value = (obj == null) ? "" : obj.toString();
              return value;

  • Printing text on the back of each page

    I have a question about printing on the "back" of each page. I am trying to create documents (like Purchase Orders, Order Acknowlegments, Invoices) that have Legal Terms and Conditions that need to print on the back of each printed page(so it needs to be every other page). I can't figure out how to make that happen with reports. My report output is pdf.
    I see that a Trailer page has a "Repeat On" property...but that is based on groups. I need it to be based on pages. Even so, they all print at the end.
    I also see there is a between_pages report trigger...can I somehow use that?
    Using Reports 10.1.2.0.2 with Unix App Server(hp-ux).

    Hi curiouslyfun,
    You wrote:
    I'm working on a long document.
    Peter wrote:
    I suspect that it may ultimately be a matter of just how much document can be held in RAM and calculated as to its layout, something Pages is not noticeably efficient at doing.
    Ahem.
    Pages is a $20 word processor. It is not a clone of Microsoft Word. Pages is a user-friendly app for short documents. If you need strong horsepower, buy and use Word for Mac.
    Popcorn wrote:
    an editor will get the document. They requested pages.
    Hi Pop, perhaps your editor will accept your work in a number of small Pages documents. Split your work into several "chapters" in separate Pages docs. If your editor is a Pages user, they will understand.
    Ian.

  • Preview displays PDF with "COPY" written across each page

    Canon's saving money by no longer including the User Guide with some of their camera's such as the Canon A1100 IS:
    If I go to the canon site:
    http://www.usa.canon.com/consumer/controller?act=ModelInfoAct&fcategoryid=183&mo delid=18138#DownloadDetailAct
    ... I can download the User Guide as a PDF from here:
    http://gdlp01.c-wss.com/gds/6/0300001856/01/PSA1100ISCUGEN.pdf
    In Preview 5.0.1 (503), every page of the manual has the word "COPY" written across every page, which interferes with legibility.
    If I open the same PSA1100ISCUGEN.pdf file in Acrobat Pro 9, then the word "COPY" is no longer displayed.
    However if I go to print the manual from Acrobat Pro 9, then the preview in the Print Dialog shows the word "COPY" across each page to be printed.
    So whether I use OS X's Preview.app, or Acrobat Pro 9, they both share the same printing subsystem which wants to print "COPY" across every page.
    BTW Preview.app's "Show Inspector" identifies the Canon User Guide as protected, but with Printing privileges.
    Any guidance on how to print the Canon User Guide without the word "COPY" across every page? Any thoughts on why Preview.app displays the word "COPY" onscreen, while Adobe Acrobat 9 does not? Any thoughts on whether there's a way around the common OS X printing subsystem that both sit on, wanting to print "COPY" across every page?
    PS. I'm using an Canon IP5000 ink jet, but the behavior seems the same if I'm pointing to my office's Kyocera Postscript printer.

    Search for the manual on the european site http://canon-europe.com/ - the versions there (at least for the A1100 IS) do not have the annoying COPY watermark.
    The direct link for the A1100 IS manual is http://files.canon-europe.com/files/soft32765/manual/PSA1100isCUGEN.pdf
    Thanks to a post on dpreview for the hint - http://forums.dpreview.com/forums/read.asp?forum=1010&message=34633050
    -dh

  • Printing current date and time on each page

    I'm often printing several versions of a document (e.g. a paper in progress). To keep track of the printouts I would like to print the date and time of printing onto each page, regardless of which application I'm using (although it would be okay if it works for PDF files only, if it happens automatically). I have seen Windows printer drivers including this feature, but haven't found any settings here (e.g., Canon iR3225) nor any hidden-CUPS-feature-I-could-activate-in-the-config-file.
    Any ideas? Some sort of preprocessing I could inject into cups?

    I agree with Greg. I had a quick look through the cups settings and could not see what you want. The closest thing would be a cover page, which you can also do via the standard print dialog.
    And checking the iR32xx series printer driver on Windows 7, there is no feature to print the date/time within the driver. So as Greg has mentioned your reference to Windows having this feature must be via an application rather than a driver setting.

  • Print JTable with column heading

    Hi,
    I'm very new to this JTable. I'm try to print a Jtable using the print() function (from JDK 1.5)
    JTable.print(JTable.PrintMode.FIT_WIDTH, new MessageFormat(
    _tabledata.getTitle() ),
    new MessageFormat("Page {0,number}"));
    The problem I have is that some time it print and other time it doens't print. Also, if it doesn't print, then the program become very slow or not respond. Is that the probelm with the new JDK or am I doing something wrong?
    Thanks for you help.

    Don't rely on JTable.print() methods too much.
    Sadly Sun didn't think anyone would need to print anything from java so support was added late and half heartedly (programmers hate printing stuff)
    If you are new to java you need to focus on something simpler than printing documents; unfortunatly printing is a tedious burdonsome task for experreineced developers
    for example: learn how to output your data from a table into an HTML formate/file; you can build beautiful reports/printouts easily and view them in java components easily but you will probably want to print them from a browser.
    Even if you find a class or two to help with your printing efforts on the net you will find you need to know many other generic complicated aspects of java to continue
    Sean

  • Laserjet 6P won't stop printing - prints a line of junk on each page -not connected to network or PC

    My HP Laserjet 6P won't stop printing.  It prints a few lines of symbols and characters at the top of each page.  The printer is not connected to a network or computer.  I tried shutting the printer off for a long time.   This started when I tried printing from a MAC to the printer, but now there is nothing connected to the printer.
    Any suggestions for resetting, etc.?   I don't know how to reset this printer so I haven't tried anything with the various buttons, etc.

    Disable the "Enable bidirectional support" in the P1102w Properties > Ports.
    Please mark the post that solves your issue as "Accept as Solution".
    If my answer was helpful click the “Thumbs Up" on the left to say “Thanks”!
    I am not a HP employee.

  • Auto adjustment of page orientation causes printing issues with documents that have a page width greater than its page height.

    Hi,
    Since an upgrade from Word 2003 to Word 2010, we are experiencing printing issues with certain
    documents that have a page width greater than its page height.
    In Word 2003, it was no problem to set the page width greater than the page height while setting the page orientation to "portrait".
    In Word 2010, this seems impossible: despite leaving the page orientation to "portrait", if one changes the page width/height as stated,
    Word 2010 automatically adjusts the orientation to "landscape". Resetting the orientation to "portrait" will flip the page's width/heigth settings.
    This behaviour causes printing problems: the page prints out 90° rotated.
    I do not believe this is a printer driver issue: I tried several different printer drivers, and all give the same (bad) result. Setting the printer driver's page orientation settings (before printing) makes no difference, as
    Word 2010 seems to force the page orientation settings.
    Inserting the documents 90° rotated is not an option, since it causes problems with the printing margins.
    This is very annoying, since we have to print official documents of a municipality (driver's licences).
    Is there a workaround for this issue?
    Regards,
    Laurent Grandgaignage
    Sysadmin Gemeentebestuur Stabroek, Belgium

    Hi
    Thank you for using
    Microsoft Office for IT Professionals Forums.
    From your description, we can follow these steps to test this issue
    Step 1: Repair Office 2010
    1.      
    Click
    Start, and then click Control Panel.
    2.      
    Click
    Programs and Features.
    3.      
    Click the
    Office 2010 program that you want to repair, and then click
    Change.
    4.      
    Click
    Repair, and then click Continue. You might need to restart your computer after the repair is complete.
    Step 2:
    Rename the global template (Normal.dotm)Follow the steps for the operating system that you are using:
    Windows Vista and Windows 7
    a)      
    Exit Word 2010
    b)      
    Click
    Start.
    c)       
    In the
    Start Search box, type the following text, and then press
    ENTER:
    1.      
    %userprofile%\appdata\roaming\microsoft\templates
    d)      
    Right-click
    Normal.dotm, and then click Rename.
    e)      
    Type
    Oldword.old, and then press ENTER.
    Microsoft Windows XP
    a)      
    Exit Word 2010
    b)      
    Click
    Start, and then click Run.
    c)       
    In the
    Open box, type the following text, and then press ENTER:
    %userprofile%\Application Data\Microsoft\Templates
    d)      
    Right-click
    Normal.dotm, and then click Rename.
    e)      
    Type
    Oldword.old, and then press ENTER.
    f)       
    Close Windows Explorer.
    How to troubleshoot print failures in Word 2010, Word 2007, and Word 2003
    http://support.microsoft.com/kb/826845
    Please take your time to try the suggestions and let me know the results at your earliest convenience. If anything is unclear or if there is anything
    I can do for you, please feel free to let me know.
    Hope that helps.
    Sincerely
    William Zhou CHNPlease remember to mark the replies as answers if they help and unmark them if they provide no help.

  • Print JTable with row headers

    I am using the fancy new printing capablities in java 1.5 to print my JTable and wow is it ever slick!
    PrintRequestAttributeSet set = new HashPrintRequestAttributeSet();
    set.add(OrientationRequested.LANDSCAPE);
    this.matrixJTable.print(JTable.PrintMode.NORMAL, null, null, true, set, false);Its just that easy. Way to go sun!
    The one problem that I am encountering is that my row headers don't print. The problem is that JTables don't support row headers, you have to use a JScrollPane for that.
    I need a way to print my JTable so that the row headers show up in the printout... and hopefully still use the warm and fuzzy new printing capabilities of JTable printing in java 1.5.
    (ps/ Isn't it time to add row header support to JTables?)

    The problem is that JTables don't support row headers, you have to use a JScrollPane for that.Well technically JTable's don't really support column headers either. It is a seperate component (JTableHeader). A JTable will automatically add its table header to the table header area of a JScrollPane. (but you don't have to use a jscrollpane to see the column headers, it is just the quickest and easiest way).
    Really shouldn't be hard to implement a row header and manually add it to the scroll panes row header area or use a BorderLayout and put your row header in the WEST and put your table in the CENTER if you don't want a scroll pane.
    Of course this won't help you with your printing issue.

  • Print JTable with multi line header

    I need to print a JTable with multi line header, I want to know if I can use the method jTable.print(int, MessajeFormat, MessageFormat) by manipulation of the MessageFormat. How I can manipulate it?
    Otherwise, How I can print this?

    hi again,
    To print pdf in a swing application you don't need servlet.jar.
    You'll only need itext.jar and a printer connected to your pc.
    Download the iText source code and unzip it. See the following classes:
    com.lowagie.tools.LPR and com.lowagie.tools.BuildTutorial. This latter is the main class of a swing tool that you can run.
    Silent Print:
    You have only to embed this javascript code in your pdf:
    writer.addJavaScript("this.print(false);", false);
                        document.add(new Chunk("Silent Auto Print"));Then, you have to send the document to the printer.
    Google : java print pdf
    http://forum.java.sun.com/thread.jspa?threadID=523898 or
    http://www.exampledepot.com/egs/javax.print/pkg.html for printing task.
    Under unix system, I used this:
                           String PRINTER = ...;
                   try {
                        String cmd = "lp -d " + PRINTER + " " + PDF_PATH;
                        Runtime.getRuntime().exec(new String[] { "sh", "-c", cmd });
                   } catch (Exception e) {
                                 //handle the exception
                                 e.printStackTrace();
                   }hth

  • How to print jTable with custom header and footer....

    Hello all,
    I'm trying to print a jTable with custom header and footer.But
    jTable1.print(PrintMode,headerFormat,footerFormat,showPrintDialog,attr,interactive)
    does not allow multi line header and footer. I read in a chat that we can make custom header and footer and wrap the printable with that of the jTable. How can we do that..
    Here's the instruction on the chat...
    Shannon Hickey: While the default Header and Footer support in the JTable printing won't do exactly what you're looking for, there is a straight-forward approach. You can turn off the default header/footer and then wrap JTable's printable inside another Printable. This wrapper printable would then render your custom data, and then adjust the size given to the wrapped printable
    But how can i wrap the jTable's Printable with the custom header and footer.
    Thanks in advance,

    I also once hoped for an easy way to modify a table's header and footer, but found no way.
    Yet it is possible.

  • Inserting section title on each page

    Is there a way to insert the title of the section automitacally at top (or bottom) of each page?
    When I insert "section title," it actually inserts the "book title," from the document section of the inspector.
    Thanks.
    b

    I'm using my own, simple template. I've created a template for all sections, and a template for the pages in each section, to which I've attached the name of that section in the header.
    The odd thing is that if I go into the "book" section and pick a page template on which I've put a text box as a header, I can double-click the textbox and I'm given an option to "insert section title and/or number." That would suggest that I can have the section name in the header of each page in the section, but somehow I've not made the correct link, and all I get when I click "insert section name" is the name of the book.
    For now, I'm not going into this further: Either I really don't understand the method behind the organization of these templates, or there the method isn't worked out. I'm sure I've missed something, but can't spend the time figuring it out, so I've done it manually. More digging around is just taking time away from me and also bothering helpful people like you.
    Thanks very much for the input.

  • Printing JTable with DefaultTableCellRenderer (or JLabel's)

    Hi there,
    I am trying to print a JTable by having it implement the Printable interface. It works, but it works only because I use the string values from the renderers of the cells. This works fine if its just simple left alligned strings.
    But when you are using formatted text strings for instance (in a DefaultTableCellRenderer extension) or something that represents a boolean (a checkbox), it doesn't look very good. Bad actually. I tried to use the print method on the renderer or cast them first to a JComponent or JLabel and then use the print method. No success so far. Can anybody help me? I am quit desperate right now.
    Kind regards,
    Ren�

    Yes I can. I took the print method from the class in which I implemented it (the JTable is a member of that particular class):
    * Implementing the Printable interface.
    * For printing the same font is used as is used on the screen.
    * So, no special print font is used or can be choosen at this
    * point. Also the font size is taken from the table on screen.
    * The title font is twice the font size of the normal font, but
    * bold. The column headers have the same font as the normal font
    * but also bold like the title.
    * The height we'll be using for a line of text will be 1.5 times
    * the average height in this font, with a minimum of 10.
    * The title font, the table header font and the table content font
    * are all the same (unlike the table) and based on the table font.
    * The table header font is not used.
    * If the table does not fit on paper, one could adjust the orientation
    * of the paper in the paper dialog, or adjust the distance bewtween
    * the columns on the screen as the column width on the screen is
    * taken to the paper.
    public int print(Graphics pg, PageFormat pageFormat, int pageIndex) throws PrinterException {
    if (pageIndex >= m_maxNumPage) {
    return NO_SUCH_PAGE;
    // This is for anti aliasing.
    Graphics2D g2d = null;
    if (pg instanceof Graphics2D) {
    g2d = (Graphics2D) pg;
    g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON));
    g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON));
    pg.translate((int) pageFormat.getImageableX(), (int) pageFormat.getImageableY());
    int wPage = (int) pageFormat.getImageableWidth();
    int hPage = (int) pageFormat.getImageableHeight();
    // This will make sure no printing is
    // done outside the designated area.
    pg.setClip(0, 0, wPage, hPage);
    // Forget this: its a test (works fine though).
    //table.print(g2d);
    //if (true)
    // return PAGE_EXISTS;
    int y = 0;
    // Make the title font size twice the size of the regular font size.
    pg.setFont(this.table.getFont().deriveFont(Font.BOLD, (this.table.getFont().getSize() * 2)));
    pg.setColor(Color.black);
    FontMetrics fm = pg.getFontMetrics();
    y += fm.getAscent();
    // Draw the title, the date and the page number.
    pg.drawString(pageTitle + " " + DateFormat.getDateInstance(DateFormat.MEDIUM, this.locale).format(new Date(System.currentTimeMillis())) + " (" + (pageIndex + 1) + ") ", 0, y);
    //          pg.drawString(pageTitle + " " + DateFormat.getDateInstance().format(new Date(System.currentTimeMillis())) + " (" + (pageIndex + 1) + ") ", 0, y);
    y += 20; // space between title and table headers
    // The font for the table headers.
    Font headerFont = this.table.getTableHeader().getFont();
    pg.setFont(headerFont);
    fm = pg.getFontMetrics();
    TableColumnModel colModel = this.table.getColumnModel();
    int nColumns = colModel.getColumnCount();
    int x[] = new int[nColumns];
    x[0] = 0;
    int h = fm.getAscent();
    // Add ascent of header font because of baseline positioning.
    y += h;
    int nRow, nCol;
    // Draw the COLUMN HEADERS.
    for (nCol = 0; nCol < nColumns; nCol++) {
    TableColumn tk = colModel.getColumn(nCol);
    int width = tk.getWidth();
    String title = (String) tk.getIdentifier();
    // The x offset for each column stored.
    if (nCol + 1 < nColumns) {
    x[nCol + 1] = x[nCol] + width;
    // If this is not the last column header, calculated
    // how many characters can be printed, based on the
    // width of the column being printed.
    if ((nCol < (nColumns - 1)) && title.length() > 0) {
    //if (x[nCol] + width <= wPage) {
    for (int i = title.length(); i >= 0; i--) {
    if ((fm.stringWidth(title.substring(0, i))) <= width) {
    // Adjust the colum header for the space available (the width).
    title = title.substring(0, i);
    break;
    // Draw the column header.
    pg.drawString(title, x[nCol], y);
    pg.setFont(this.table.getFont());
    fm = pg.getFontMetrics();
    int header = y;
    // Gets the standard height of a line of text in this font.
    // This is the distance between the baseline of adjacent lines of text.
    // It is the sum of the leading + ascent + descent. There is no guarantee
    // that lines of text spaced at this distance are disjoint; such lines may
    // overlap if some characters overshoot either the standard ascent or the
    // standard descent metric.
    h = fm.getHeight();
    // Calculate the ACTUAL height we'll be using for a line of text.
    // That would be 1.5 times the average height, with a minimum of 10.
    // Actually, normally, the average height should suffice. But we're
    // not taking any risks here.
    int rowHeight = Math.max((int) (h * 1.5), 10);
    // Calculate how many rows fit on a page.
    int rowPerPage = (hPage - header) / rowHeight;
    m_maxNumPage = Math.max((int) Math.ceil(this.table.getRowCount() / (double) rowPerPage), 1);
    int iniRow = pageIndex * rowPerPage;
    int endRow = Math.min(this.table.getRowCount(), iniRow + rowPerPage);
    String cell = null;
    // Draw every ROW.
    for (nRow = iniRow; nRow < endRow; nRow++) {
    y += rowHeight;
    // Draw each CELL in each ROW.
    for (nCol = 0; nCol < nColumns; nCol++) {
    cell = null;
    int col = this.table.getColumnModel().getColumn(nCol).getModelIndex();
    // Get the cell object. This is the object that needs to be printed.
    Object object = model.getValueAt(nRow, col);
    // RENDERING FOR PRINTING! Here is determined for the printing how the rendering is done.
    // If a special renderer had been set an attempt is made to use that renderer (e.g. get the text).
    // If that is not possible, simple rendering: the toString() value.
    // BE AWARE! The format for numbers, booleans etc. might require better code.
    if (table.getColumnModel().getColumn(nCol).getCellRenderer() != null) {
    if (table.getColumnModel().getColumn(nCol).getCellRenderer().getTableCellRendererComponent(table, object, false, false, nRow, nCol) != null) {
    if (table.getColumnModel().getColumn(nCol).getCellRenderer().getTableCellRendererComponent(table, object, false, false, nRow, nCol) instanceof JLabel) {
    cell = ((JLabel) table.getColumnModel().getColumn(nCol).getCellRenderer().getTableCellRendererComponent(table, object, false, false, nRow, nCol)).getText();
    //table.getCellRenderer(0, nCol).getTableCellRendererComponent(table, object, false, false, 0, nCol).paint(g2d);
    // Object fiets = table.getCellRenderer(0, nCol).getTableCellRendererComponent(table, object, false, false, 0, nCol);
    // if (fiets instanceof DefaultTableCellRenderer) {
    //      System.out.println("DefaultTableCellRenderer!" + nCol);
    //      ((DefaultTableCellRenderer) fiets).print(pg);
    // If the object is a Boolean, print an "X" or an "O".
    if (object instanceof Boolean) {
    cell = (((Boolean) object).equals(new Boolean(true)) ? "X" : "O");
    // Still don't know how to print it? Use the object's toString() method.
    // This is probably used in most cases anyway!
    if (cell == null && object != null) {
    //cell = object.toString();
    if (cell == null) {
    cell = "";
    // If this is not the last column, calculated how many
    // characters can be printed, based on the width of
    // the column being printed.
    if ((nCol < (nColumns - 1)) && cell.length() > 0) {
    int width = colModel.getColumn(nCol).getWidth();
    for (int i = cell.length(); i >= 0; i--) {
    if ((fm.stringWidth(cell.substring(0, i))) <= width) {
    // Adjust the size.
    cell = cell.substring(0, i);
    break;
    // Print the cell.
    pg.drawString(cell, x[nCol], y);
    System.gc();
    return PAGE_EXISTS;
    }

Maybe you are looking for

  • Improving Ibook to TV picture quality?

    Hi. I have tried hooking my ibook G4 with my TV (32" CRT) as an external display using the video adapter and a composite video cable. Its really only good for viewing pics and video. Web pages and print are unreadable. Is there any way of improving t

  • How to change browser tab text?

    When I open (view) my WebHelp project, the title is not correct. It is using an "old" title from a previous project that this one was created from. I changed the title on the Properties dialog box in the actual RoboHelp project, under Project Setting

  • Why do all my apps automatically shut down as soon as I open them?

    Whenever I open any app on my ipad (netflix, skype, Kayak) they automatically. What can i do?

  • SAP Carbon Impact Testing Document

    Hi, I am currently working on SAP Carbon Impact. I am just preparing the testing documents for SAP carbon impact report. I am confused about the contents of the testing document. What should be testing criteria for a SAP cabon impact report and what

  • HT1918 How do I change the answers to my security questions?

    I forgot my answers for my security questions because I set up my AppleID about a hundred years ago and now I can't make any purchases until I answer them, how can I reset my questions/answers?