开发者

Truncated JTable print output

开发者 https://www.devze.com 2023-02-08 05:52 出处:网络
I have a JTable that uses JTextArea as its TableCellRenderer, so that table cells can utilize word wrap.The JTable displays fine.When I print the table to a printer via JTable\'s print method, the out

I have a JTable that uses JTextArea as its TableCellRenderer, so that table cells can utilize word wrap. The JTable displays fine. When I print the table to a printer via JTable's print method, the output is always truncated at approximately 60% of the data. I have tried different computers and different printers, and different printer drivers, different JVM versions (1.5, 1.6) but none of that has helped. Below is a self-contained Java main class that reproduces the problem. Any ideas?

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

public class JTextAreaJTableTest extends javax.swing.JFrame {

    public static void main(开发者_如何学运维String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                JTextAreaJTableTest frame = new JTextAreaJTableTest();
                frame.setSize(640, 480);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    JButton jButtonPrint;
    JScrollPane jScrollPane;
    JTable jTable;
    JToolBar jToolBar;

    public JTextAreaJTableTest() {
        initComponents();

        DefaultTableModel dtm = (DefaultTableModel) jTable.getModel();
        Vector<Vector<String>> data = new Vector<Vector<String>>();
        for (int i = 0; i < 50; i++) {
            Vector<String> rowData = new Vector<String>();
            rowData.add("Entry " + i);
            rowData.add("Lorem ipsum dolor sit amet, consectetur adipisicing"
                    + " elit, sed do eiusmod tempor incididunt ut labore et"
                    + " dolore magna aliqua. Ut enim ad minim veniam, quis"
                    + " nostrud exercitation ullamco laboris nisi ut aliquip"
                    + " ex ea commodo consequat. Duis aute irure dolor in"
                    + " reprehenderit in voluptate velit esse cillum dolore"
                    + " eu fugiat nulla pariatur. Excepteur sint occaecat"
                    + " cupidatat non proident, sunt in culpa qui officia"
                    + " deserunt mollit anim id est laborum. " + i);
            data.add(rowData);
        }
        Vector<String> columnNames = new Vector<String>();
        columnNames.add("Key");
        columnNames.add("Value");
        dtm.setDataVector(data, columnNames);
        jTable.setDefaultRenderer(String.class, null);
        jTable.getColumnModel().getColumn(0).setCellRenderer(
                new TextAreaCellRenderer());
        jTable.getColumnModel().getColumn(1).setCellRenderer(
                new TextAreaCellRenderer());
    }

    private void initComponents() {
        jToolBar = new JToolBar();
        jButtonPrint = new JButton();
        jScrollPane = new JScrollPane();
        jTable = new JTable();

        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        jToolBar.setRollover(true);

        jButtonPrint.setText("Print");
        jButtonPrint.setFocusable(false);
        jButtonPrint.setHorizontalTextPosition(SwingConstants.CENTER);
        jButtonPrint.setVerticalTextPosition(SwingConstants.BOTTOM);
        jButtonPrint.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                jButtonPrintActionPerformed(evt);
            }
        });
        jToolBar.add(jButtonPrint);

        getContentPane().add(jToolBar, BorderLayout.NORTH);

        jScrollPane.setViewportView(jTable);

        getContentPane().add(jScrollPane, BorderLayout.CENTER);
    }

    private void jButtonPrintActionPerformed(ActionEvent evt)                                             
    {                                                 
        try {
            jTable.print();
        } catch (PrinterException ex) {
            ex.printStackTrace();
        }
    }                                            

    public static class TextAreaCellRenderer extends JTextArea implements
            TableCellRenderer {

        public TextAreaCellRenderer() {
            this.setLineWrap(true);
            this.setWrapStyleWord(true);
        }

        public Component getTableCellRendererComponent(JTable table, 
                Object value, boolean isSelected, boolean hasFocus,
                int row, int column) {
            this.setText(String.valueOf(value));
            TableColumnModel columnModel = table.getColumnModel();
            this.setSize(columnModel.getColumn(column).getWidth(), 1);
            int newHeight = this.getPreferredSize().height;
            if (newHeight > table.getRowHeight(row)) {
                table.setRowHeight(row, this.getPreferredSize().height);
            }
            return this;
        }
    }
}


I believe I found the root cause and a solution. The print problem stems from the fact that the table is the wrong height. The table is the wrong height because some of the table's rows are the wrong height. Some of the rows are the wrong height because their cell renderers have not been invoked by JTable (that is, their getTableCellRendererComponent method has not been called.) The reason JTable is skipping some of the cell renderers is optimization -- the cell renderers it skips are for cells that are offscreen -- offscreen means rendering isn't needed; JTable's optimization in this area makes sense.

However, the renderers are the only place in the program where the correct height of each individual row is set. My choice to set the row height in the cell renderer is somewhat understandable because the cell renderer is in the best position to know what height the row should be.

It seems the most straight-forward way to fix this example program is to manually call getTableCellRendererComponent on every cell renderer (this should be done after modifying the table's model data.) This gives the renderers an opportunity to set every single row in the table to its correct individual height. With each row at its correct height, the JTable overall ends up being the correct height, which seems to resolve the print truncation issue. The following code shows visiting all the renderers to do just that:

for (int colIdx = 0; colIdx < jTable.getColumnCount(); colIdx++)
{
    for (int rowIdx = 0; rowIdx < jTable.getRowCount(); rowIdx++)
    {
        TableColumnModel columnModel = jTable.getColumnModel();
        TableColumn column = columnModel.getColumn(colIdx);
        TableCellRenderer renderer = column.getCellRenderer();
        Object cellValue = jTable.getValueAt(rowIdx, colIdx);
        renderer.getTableCellRendererComponent(jTable, cellValue, 
                    false, false, rowIdx, colIdx);
    }
}

This manual visitation of all cell renderers bypasses JTable's optimization of only invoking the cell renderers of on-screen cells. As such, you may want to only execute this workaround code after the user has requested printing (but before calling the print method on the JTable). This lets the JTable optimization stay in effect if the user is just using the GUI and not printing.


I had to slightly modify Mike Clarks solution to make it work for me:

for (int colIdx = 0; colIdx < jTable.getColumnCount(); colIdx++) {
    for (int rowIdx = 0; rowIdx < jTable.getRowCount(); rowIdx++) {
        TableCellRenderer renderer = jTable.getCellRenderer(rowIdx, colIdx);
        Object cellValue = jTable.getValueAt(rowIdx, colIdx);
        renderer.getTableCellRendererComponent(jTable, cellValue,
                false, false, rowIdx, colIdx);
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消