Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3931714
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T23:22:42+00:00 2026-05-19T23:22:42+00:00

I have a JTable that uses JTextArea as its TableCellRenderer, so that table cells

  • 0

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;
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T23:22:43+00:00Added an answer on May 19, 2026 at 11:22 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JTable which uses a custom TableModel to display a series of
I have a JTable that is within a JScrollPane. Rows are added to the
I have a JTable that I want to use to display some data (a
I have a JTable that stores the results of a database query, so far
Good evening I have a JTable that I built with a TableModel how to
I have an application that uses Swing. The display I am working on, uses
I have a script that appends some rows to a table. One of the
I have a JTable such that: ID #: Name: 0 Entry 0 1 Entry
I have a JTable and a TableModel that extends AbstractTableModel . I would like
I have a JTable with a custom TableModel called DataTableModel . I initialized the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.