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

  • SEARCH
  • Home
  • 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 6731199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:29:59+00:00 2026-05-26T10:29:59+00:00

I have a JTable with a custom Cell Renderer for multi-line cells. Everything is

  • 0

I have a JTable with a custom Cell Renderer for multi-line cells. Everything is ok, the JTable is painted ok in the screen and I am very happy with it, but ast night when I tried to simply print it, I came up with a very strange issue. Using:

table.print(PrintMode.FIT_WIDTH, new MessageFormat(“…”), new MessageFormat(“…”));

I saw that the table did not print entirely. Then using another class made from a colleague for printing JTables I had the same result:

The table (with multi-line cells) needed 22 pages to print. The printed document (which I only viewed in xps format since I do not own a printer) had also 22 pages. But up to page 16 everything was printed as expected and after that only the borders and the column headers of the table were printed.

Strangely (to me) enough, when I tried to print the table using another cell renderer that does not allow for multi line cells, the table needed exactly 16 pages and was printed entirely, albeit the cropping in the lengthy cell values.

I searched all over the net but I had no luck. Does anybody know why could this be happening? Is there a solution?

Update:

My cell renderer is the following:

public class MultiLineTableCellRenderer extends JTextPane implements TableCellRenderer {
private List<List<Integer>> rowColHeight = new ArrayList<List<Integer>>();

public MultiLineTableCellRenderer() {
    setOpaque(true);
}

public Component getTableCellRendererComponent(
        JTable table, Object value, boolean isSelected, boolean hasFocus,
        int row, int column) {
    String s = (String)value;
    if (s.equals("<περιοδάριθμος>")) {
        setForeground(Color.blue);
    }
    else if(s.equals("<παραγραφάριθμος>")) {
        setForeground(Color.red);
    }
    else {
        setForeground(Color.black);
    }
    setBackground(new Color(224, 255, 255));
    if (isSelected) {
         setBackground(Color.GREEN);
    }
    setFont(table.getFont());
    setFont(new Font("Tahoma", Font.PLAIN, 10));
    if (hasFocus) {
        setBorder(UIManager.getBorder("Table.focusCellHighlightBorder"));
        if (table.isCellEditable(row, column)) {
            setForeground(UIManager.getColor("Table.focusCellForeground"));
            setBackground(UIManager.getColor("Table.focusCellBackground"));
        }
    } else {
        setBorder(new EmptyBorder(1, 2, 1, 2));
    }
    if (value != null) {
        setText(value.toString());
    } else {
        setText("");
    }
    adjustRowHeight(table, row, column);

    SimpleAttributeSet bSet = new SimpleAttributeSet();
    StyleConstants.setAlignment(bSet, StyleConstants.ALIGN_CENTER);
    StyleConstants.setFontFamily(bSet, "Tahoma");
    StyleConstants.setFontSize(bSet, 11);
    StyledDocument doc = getStyledDocument();
    doc.setParagraphAttributes(0, 100, bSet, true);
    return this;
}

private void adjustRowHeight(JTable table, int row, int column) {
    int cWidth = table.getTableHeader().getColumnModel().getColumn(column).getWidth();
    setSize(new Dimension(cWidth, 1000));
    int prefH = getPreferredSize().height;
    while (rowColHeight.size() <= row) {
        rowColHeight.add(new ArrayList<Integer>(column));
    }
    List<Integer> colHeights = rowColHeight.get(row);
    while (colHeights.size() <= column) {
        colHeights.add(0);
    }
    colHeights.set(column, prefH);
    int maxH = prefH;
    for (Integer colHeight : colHeights) {
        if (colHeight > maxH) {
            maxH = colHeight;
        }
    }
    if (table.getRowHeight(row) != maxH) {
        table.setRowHeight(row, maxH);
    }
}

}

Furthermore, if you test the following very simple example you will notice that something is terribly wrong with the printing, but I really can’t find what!

public static void main(String[] args) throws PrinterException {
  DefaultTableModel model = new DefaultTableModel();
  model.addColumn("col1");
  model.addColumn("col2");
  model.addColumn("col3");
  int i = 0;
  for (i = 1; i <= 400; i++) {
     String a = "" + i;
     model.addRow(new Object[]{a, "2", "3"});
  }
  JTable tab = new JTable(model);
  tab.print();
}
  • 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-26T10:29:59+00:00Added an answer on May 26, 2026 at 10:29 am

    I believe you are having the same problem that I had when I asked this question:

    Truncated JTable print output

    I found a solution to my problem, and I believe it may help you as well.
    The answer is here:
    Truncated JTable print output

    To summarize my answer:

    If your TableCellRenderer is the only place in your code where you are setting rows to their correct height, then you are going to run into trouble caused by an optimization inside JTable: JTable only invokes TableCellRenderers for cells that have been (or are about to be) displayed.

    If not all of your cells have been displayed on-screen, then not all of your renderers have been invoked, and so not all of your rows have been set to the desired height. With your rows not being their correct height, your JTable overall height is incorrect. After all, part of determining the overall JTable height is accounting for the height of each of that table’s rows. If the JTable overall height isn’t correct, this causes the print to truncate, since the JTable overall height is a parameter that is considered in the print layout logic.

    An easy (but perhaps not squeaky clean) way to fix this is to visit all of your cell renderers manually before printing. See my linked answer for an example of doing this. I actually chose to do the renderer visitation immediately after populating my table with data, because this fixes some buggy behavior with the JTable’s scrollbar extents (in addition to fixing the printing.)

    The reason the table looks and works OK on-screen even when printing is broken, is because as you scroll around in the table, the various renderers are invoked as new cells come on screen, and the renderers set the appropriate row height for the newly visible rows, and various dimensions are then are recalculated on the fly, and everything works out OK in the end as you interact with the table. (Although you may notice that the scrollbar “extent” changes as you scroll around, which it really shouldn’t normally do.)

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

Sidebar

Related Questions

I have a custom cell renderer set in JTable and it works but instead
I have a JTable with a custom cell renderer. The cell is a JPanel
I have my on custom cell renderer and want to remove the border of
I have a JTable in Java that has a custom dataMOdel and custom renderer.
I have a custom TableCellRenderer (ValueRenderer) for a JTable, the cell is a Checkbox
I have a JTable and i want a cell (or its row) painted in
I have a custom renderer in a JTable . When my JTable displays, I
I have a JTable which is 9 by 3 (27 cells), but if the
I have a JTable that holds several JPanels using a custom renderer/editor. The JPanel
I have a JTable in my application. I have a custom renderer setup on

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.