Java Solaris Communities Sun Store Join SDN My Profile Why Join?
 
Bug Database
Bug Detail
Quick Lists
Top 25 Bugs
Top 25 RFE's
Recently Closed Bugs
Printable Page Printable Page


Bug Database
Bug ID: 4409239
Votes 9
Synopsis Printing in JTable clips cell contents
Category java:classes_swing
Reported Against 1.3
Release Fixed
State 11-Closed, duplicate of 4352983, bug
Priority: 4-Low
Related Bugs 4352983
Submit Date 29-JAN-2001
Description




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) 
======================================================================
Work Around




No workaround found
======================================================================
Evaluation
This has been fixed by 4352983.
  xxxxx@xxxxx   2004-04-26
Comments
  
  Include a link with my name & email   


PLEASE NOTE: JDK6 is formerly known as Project Mustang