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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:53:03+00:00 2026-05-16T10:53:03+00:00

Before any one suggests HTML, I explain later why thats not an option here.

  • 0

Before any one suggests HTML, I explain later why thats not an option here. I have a table that contains a column with text cells in it. I need to be able to highlight some of the text in each cell. So for example if the cell contained “cat foo dog”… I might want to highlight foo.

My current method is to use a custom TableCellRenderer that puts html into a JLabel component that gets rendered and for a time it was good. Then I noticed that when the text in the cell became too long to fit in the column width it just truncated the text without the normal “…” that happens normally in this case. Thus users didnt know there was more text they were not seeing. The other problem was that if the original text itself contained HTML, which in my case it does at times, the cell would not render correctly. I know I could just escape the html but I would still have the prevous problem.

If I use a component other than a jlabel then it makes my table’s cells look strange. Does any one have any suggestions? Thanks

  • 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-16T10:53:04+00:00Added an answer on May 16, 2026 at 10:53 am

    Well, here is a solution.

    In short, you can subclass JLabel to draw the highlight manually. Override the paintComponent method to do the actual drawing and use FontMetrics to calculate where the highlighted region should be drawn.

    Here is that answer in excruciating detail:

    Basically, you can make a subclass of JLabel that can highlight stuff. I would do that like this; you may want to do it somewhat differently:

    Add a method that tells the label which part to highlight. This could be something like this, assuming you just need one highlighted region:

    public void highlightRegion(int start, int end) {
         // Set some field to tell you where the highlight starts and ends...
    }
    

    If you need multiple regions, just have an ArrayList instead of a simple field. A method for dehighlighting would probably be useful too.

    Now, you need to override the paintComponent method of JLabel. Here you need to do several discrete steps, which you may want to organize in different methods or something. For simplicity, I’ll put it all in the paint method.

    @Override
    protected void paintComponent(Graphics g) {
      ...
    

    First, you need to figure out the physical dimensions of the highlight, which you can do using the nice FontMetrics class. Create the FontMetrics class for the Font you’re using.

      FontMetrics metrics = new FontMetrics(getFont());
    

    Now you can get all the information you need to create a rectangle that will be the highlight. You’ll need the starting position, the height and the width. To get this, you’ll need two substrings of the JLabel‘s text as follows:

      String start = getText().substring(0, startOfHighlight);
      String text = getText().substring(startOfHighlight, endOfHighlight);
      //You may also need to account for some offsets here:
      int startX = metrics.stringWidth(start);
      int startY = 0; //You probably have some vertical offset to add here.
      int length = metrics.stringWidth(text);
      int height = metrics.getHeight();
    

    Now you can draw the highlighted region before drawing the rest of the label:

      g.fillRect(startX, startY, length, height);
      super.paintComponent(g);
    }
    

    Of course, if you want the highlight to span multiple rows, that will require more work.

    If you were wondering, I have actually written something like this before. On a whim, I decided to write my own text area type component from a JPanel, and this was basically the way I handled highlighting. Reinventing the wheel may be stupid in an actual project, but it does teach you random stuff that may come in useful…

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

Sidebar

Related Questions

Possible Duplicate: HTML Compress File Upload? I have complains from client that my website
I am using MarkdownSharp in one of my projects and have noticed that if
(Before anyone says anything Yes this was homework but i have already turned it
Ok, before anyone attempts to label this as a duplicate, I am not asking
When one uses ALTER TABLE tab ADD col, the new column gets added to
I have some set of links and select boxes. My html code is below:
I have a site that will display a large number of members publicly with
Can any one suggest me the best and reliable CRM Software which is Open
I have a test db on godaddy, I have had a similar issue before
One of the basics of building a distributed application that uses the asynchronous communication

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.