JDK-4409239 : Printing in JTable clips cell contents
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.3.0
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_2000
  • CPU: x86
  • Submitted: 2001-01-29
  • Updated: 2004-04-26
  • Resolved: 2004-04-26
Related Reports
Duplicate :  
Description

Name: ssT124754			Date: 01/29/2001


java version "1.3.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)

1. Steps to reproduce
   a) Start the application, main class is ClippingBug. The JTable should
      fit the columns to display all the data.
   b) Select the "Print" button and print out the JTable. The text is clipped
      at the end of each table cell.
2. Source code
// BEGIN
import java.awt.*;
import java.awt.event.*;
import java.awt.print.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.border.*;

public class ClippingBug extends JFrame implements Printable {
    JTable table;

    public ClippingBug() {
        String[] [] data = {{"MAMA MAMA MAMA MAMA", "MAMA MAMA MAMA MAMA"},
     						{"MAMA MAMA MAMA MAMA", "MAMA
MAMA MAMA MAMA"}
     						};
        String[] columnNames = {"A", "B"};
        table = new JTable(data, columnNames);
        // Set table columns width to match the data width
        Component comp;
        int numCols = table.getColumnCount();
        int colWidth[] = new int[numCols];
        int columnMargin = table.getColumnModel().getColumnMargin();
        for (int i = 0; i < numCols; i++) {
            TableColumn column = table.getColumnModel().getColumn(i);
            for (int j = 0; j < table.getRowCount(); j++) {
                TableCellRenderer rend = table.getCellRenderer(j, i);
                comp = rend.getTableCellRendererComponent(table,
table.getValueAt(j, i), false, false, j, i);
                colWidth[i] = Math.max(comp.getPreferredSize().width +
columnMargin, colWidth[i]);
            }
            column.setPreferredWidth(colWidth[i]);
        }
        getContentPane().setLayout(
            new BorderLayout());
        getContentPane().add(table);
        JButton b = new JButton("print");
        b.addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    doPrint();
                }
            });
        getContentPane().add(b, BorderLayout.SOUTH);
    }

    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();
        //leave room for page number
        double pageHeight = pageFormat.getImageableHeight() - fontHeight;
        double pageWidth = pageFormat.getImageableWidth();
        double tableWidth = (double)table.getColumnModel().getTotalColumnWidth
();
        double scale = 1;
        if (tableWidth >= pageWidth) {
            scale = pageWidth / tableWidth;
        }
        double headerHeightOnPage = table.getTableHeader().getHeight() * scale;
        double tableWidthOnPage = tableWidth * scale;
        double oneRowHeight = (table.getRowHeight() + table.getRowMargin()) *
scale;
        int numRowsOnAPage = (int)((pageHeight - headerHeightOnPage) /
oneRowHeight);
        double pageHeightForTable = oneRowHeight * numRowsOnAPage;
        int totalNumPages = (int)Math.ceil(((double)table.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 - fontDesent));
        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 = 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);
        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);
        table.getTableHeader().paint(g2);
        //paint header at top
        return Printable.PAGE_EXISTS;
    }

    public void doPrint() {
        PrinterJob pj = PrinterJob.getPrinterJob();
        pj.setPrintable(this);
        if (pj.printDialog()) {
            try {
                pj.print();
            }
            catch (PrinterException ee) {
                System.out.println(ee);
            }
        }
    }

    public static void main(String args[]) {
        ClippingBug a = new ClippingBug();
        a.addWindowListener(
            new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });
        a.pack();
        a.setVisible(true);
    }
}
// END SOURCE CODE

5. Relevant Information.
  Replacing the Strings ("MAMA MAMA ....") with lowercase makes the JTable
  print correctly.

  The printing code is taken unchanged from the Java tutorial:
  
http://developer.java.sun.com/developer/onlineTraining/Programming/JDCBook/advpr
int.html#mp
(Review ID: 115952) 
======================================================================

Comments
WORK AROUND Name: ssT124754 Date: 01/29/2001 No workaround found ======================================================================
11-06-2004

EVALUATION This has been fixed by 4352983. ###@###.### 2004-04-26
26-04-2004