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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:13:41+00:00 2026-05-11T18:13:41+00:00

Suppose you have a JTable and for each cell you want to display three

  • 0

Suppose you have a JTable and for each cell you want to display three strings with different color, say value1 (red), value2 (blue), value3 (green).

I overrode the getTableCellRendererComponent of DefaultTableCellRenderer but setForeground(Color) method gives an unique color for all the string showed in the cell.

@Override
public Component getTableCellRendererComponent(JTable table, Object value,
        boolean isSelected, boolean hasFocus, int row, int column) {
    String s = table.getModel().getValueAt(row,column).toString();

    StringTokenizer st = new StringTokenizer(s," ");
    int nToken=st.countTokens();

    value1=st.nextToken();
    value2=st.nextToken();
    value3=st.nextToken();
    // so now all the values are blue...
    setForeground(Color.blue);

    return super.getTableCellRendererComponent(table, value, isSelected,
            hasFocus, row, column);

}
  • 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-11T18:13:41+00:00Added an answer on May 11, 2026 at 6:13 pm

    The default renderer for a cell table is a JLabel. This component supports HTML tags. The easiest solution you can have is to use HTML to render the Strings with different colours. So you can build a String such as:

     <html><font color="blue">A String</font><font color="red">Another String</font></html>
    

    And set it to the cell, and you don’t have to worry about the renderer.

    Most swing components allow you to use HTML, this is not only limited to tables.

    Another possibility is that you create a class that extends from JComponent, and program paintComponent() so it draws these three strings in different colors. Then you can set this component as the renderer. But this is way more complicated. Because the renderer works like a “stamp”, it will stamp the right colors when the cells are drawn. This is useful if your needs are to, for example, draw extremely complex and custom graphics on a cell.

    I would stick with using HTML if you don’t have a humongous amount of cells.

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

Sidebar

Related Questions

Suppose I have a huge list of photos and I want to display it
Suppose I have 3 language: en , tc , sc , I want to
I have a 12 filters for 12 columns on a JTable. Each filter is
Suppose we have this code for login & we want if the credential was
Suppose I have a 400K text file which I want to read from a
Suppose I have two different structures, Struct1 and Struct2. They both have different elements
Suppose you have val docs = List(List(one, two), List(two, three)) where e.g. List(one, two)
Suppose I have a bunch of static fields and I want to use them
Suppose I have a module with the methods : function1,function2,function3. I want to import
Suppose I have 3 completely different layouts for one site. The first is shown

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.