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 6130839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:54:18+00:00 2026-05-23T16:54:18+00:00

I am using JTextPane as a table cell renderer to display rich text. When

  • 0

I am using JTextPane as a table cell renderer to display rich text. When the text is too long to fit inside a cell, it is truncated. I would like to mimic the JLabel behavior, i.e. show ellipsis (…) to alert the user that part of the text is not visible. Has anyone done this before?


Solution I ended up adopting, with help from StanislavL. The algorithm works by chopping off one character at a time off the end of StyledDocument, appending “…” and comparing resulting preferred width to table cell width. This is inefficient, especially in case of very long strings, but not a problem in my case. Can be optimized. The following goes into your renderer’s getTableCellRendererComponent

    m_dummyTextPane.setDocument(doc);
    m_dummyTextPane.setSize(Short.MAX_VALUE, table.getRowHeight());        
    int width = m_dummyTextPane.getPreferredSize().width;
    int start = doc.getLength() - 1;
    while(width >= table.getColumnModel().getColumn(col).getWidth() && start>0) {  
        try {
            doc.remove(Math.min(start, doc.getLength()), 
                    doc.getLength() - Math.min(start, doc.getLength()));
            doc.insertString(start, "...", null);
        } catch (BadLocationException e) {
            e.printStackTrace();
            break;
        }
        start--;
        width = m_dummyTextPane.getPreferredSize().width;
    }
  • 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-23T16:54:18+00:00Added an answer on May 23, 2026 at 4:54 pm

    You can use this http://java-sl.com/tip_text_height_measuring.html to measure content for the fixed width. If it requires more space than available just paint something over the JTextPane.

    I like the trashgod’s idea with scroll too. (+1)

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

Sidebar

Related Questions

Assume that the back is using a JTextPane, and I would like to implement
I have a JTextPane , and I would like to output text in it
I want to build a console-like output using JTextPane. Therefore I am using a
Using Jenkins or Hudson I would like to create a pipeline of builds with
I'm using string builders to append text to my JTextPane, I've set content type
Using Rails 3.2.0 with haml and sass: I Would like to link an external
I'm using JTextPane as simple html editor. jtp=new JTextPane(); jtp.setContentType(text/html;charset=UTF-8); jtp.setEditorKit(new HTMLEditorKit()); When I
So I've created my own text pane class (extending JTextPane) and I'm using the
Using Ubuntu, I would like to create a shell script (bash) for an Ubuntu
this is kinda overkill for me.. I am using a JTextPane for a chat,

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.